子查询(in,exists)、联合查询、

使用in关键字的子查询

例如:查询游戏类型是棋牌类的游戏分数信息

select * from scores gno in

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

子查询一般不写为select * from...,因为*代表多个列增加了查询量

例如:查询没有参与5号游戏的qq(要从玩家表中查而不是分数表因为有的玩家没有玩游戏)

select user_qq from users

where user_qq in

(select user_qq from scores where gno=5)

 

使用exists关键字的子查询

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

select * from scores

where exists

(select * form users where user_name='孙悟空')

 

联合查询

特点是把多条查询语句所产生的结果集纵向连接为一体(all关键字可以把重复的数据显示出来没有就只显示一个),并且每个查询结果列数要相同否则会产生不兼容的情况

select_statement

union[all] select_statement

[union[all]select_statement]

[....n]

 

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 user_birthday is null

 

例如查询QQ号是12301玩家的所有分数并计算出总分和平均分数并显示到同一个结果集中

select user_qq,gno,score from scores where user_qq='12301'

union all

select '总分',' ',sum(score) from scores where user_qq='12301'

union all

select '平均分',' 'avg(score) from scores where user_qq='12301'

其实select关键字之后可以加上字符串字符串会被显示在查询结果中这是为了与第一个查询结果相匹配.


 

 

 

 

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值