mysql8联合查询_MySQL数据库8 -子查询,联合查询

一 使用IN关键字的子查询

问题: 查询游戏类型是'棋牌类' 的游戏的分数信息

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

思路一:采用链接查询

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

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

例:查询没有参与5号游戏的玩家QQ

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

二 使用exists 关键字的子查询

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

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

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

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

例:查询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'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值