首页 > 数据库技术 > 详细

sql语句exists,复杂语句总结

时间:2020-01-06 23:02:54      阅读:103      评论:0      收藏:0      [点我收藏+]

1、select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name
分组查询各组最大值;
牛逼之处在于where name = a.name,用这句进行分组,然后组内找出最大值。


2、select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)
分组查询各组最大值;
牛逼之处在于两组进行两两比较,找出符合条件的a值,然后在外面的查询中找到a对应数据。


3、select a.* from student a where exists (select count(*) from student where class = a.class and score > a.score having count(*) < 3) order by a.class, a.score desc
分组排序并找出最大的3个值;
牛逼之处在于两组中的每个元素进行两两对比,然后找出满足条件的a值,然后再外面的查询中找出a对应的数据。

技术分享图片

 

 技术分享图片

sql语句exists,复杂语句总结

原文:https://www.cnblogs.com/qiaoer1993/p/12158496.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!