加入收藏 | 设为首页 | 会员中心 | 我要投稿 核心网 (https://www.hxwgxz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 电商 > 正文

MySQL数据库 —子查询,联合查询

发布时间:2020-12-31 07:45:57 所属栏目:电商 来源:网络整理
导读:《MySQL数据库 —子查询,联合查询》要点: 本文介绍了MySQL数据库 —子查询,联合查询,希望对您有用。如果有疑问,可以联系我们。 导读:一 使用IN关键字的子查询1.查询游戏类型是#39;棋牌类#39; 的游戏的分数信息 游戏分数表中并未包含游戏类型信息 思路一

《MySQL数据库 —子查询,联合查询》要点:
本文介绍了MySQL数据库 —子查询,联合查询,希望对您有用。如果有疑问,可以联系我们。

导读:一 使用IN关键字的子查询1.查询游戏类型是'棋牌类' 的游戏的分数信息 游戏分数表中并未包含游戏类型信息 思路一:采用链接查询... 一 使用IN关键字的子查询

1.查询游戏类型是'棋牌类' 的游戏的分数信息

??? 游戏分数表中并未包含游戏类型信息

?? 思路一:采用链接查询

?? 思路二: 分两步进行,首先找到所以'棋牌类'游戏的编号,再以这一组编号为查询依据完成查询

?? select * from scores where gno in (select gno from games where gtype ='棋牌')

2.查询没有参与5号游戏的玩家QQ

select user_qq from users where user_qq not in (select user_qq from scores where gno=5)



二 使用exists 关键字的子查询

1.如果存在昵称为‘孙悟空’,则查询分数表中的数据

? select * from scores where exists (select * from users user_name ='孙悟空')



三 联合查询

select _statement union[all] select_statement [union[all] select_statement][...n]

作用与特点:可以把多条查询语句所产生的结果集纵向连接为一体

???????????????? 有ALL关键字可以显示全部数据(即重复的也显示出来)

???????????????? 列的数量与类型都要兼容

select user_name from users

union

select gname from games

1.查询玩家表中所有女性玩家和生日为空的玩家

select * from users where user_sex='女'

union

select * from users where user_birthday is null

<<=====>> select * from users where user_sex='女' or select * from users where user_birthday is null

2.查询qq号是‘12302’的玩家所有分数并计算出总分数和平均分数,并显示到同一结果集中

select user_qq,gno,score from scores where user_qq='12302' union all select '总分',' ',sum(score) from scores union all select '平均分',avg(score) from scores where user_qq='12302'

(编辑:核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读