当前位置:网站首页>mysql 中 in 的用法
mysql 中 in 的用法
2022-07-30 05:15:00 【m0_67392931】
1. select * from student s where s.id in (20,30);
查询id是20或者是30,等同于select * from student s where s.id = 20 or s.id = 30;
2.select * from student s where s.id in (select age from student);
查询id是age数组里面的,单个字段只能in查询结果是单行的。
很明显后面括号的 select age from student 查出来只有age这一列,假如括号的查询查出来的age是下面图列

那么此时查询就等价于select * from student s where s.id in (64,57,32,24,35,55);
3.select * from student s where (s.class , s.score) in (select class , max(score) from student group by class)
既然能单个字段in单行结果,那么多个字段就能in多行结果了。
这个sql表示:查询每个班级中分数最高的学生的所有数据
注意,此时的name和class是不会错位的,你本来就是按着匹配的class和score去in匹配class、score的结果集,所以数据不会出错的。
如果你写成这样:
select * from student s where s.classin (select class from student group by class) and s.score in (select max(score) from student group by class);
就把class和score的关系分开了,分开后就可能出现结果列错位的情况,可能名字和他的分数对不上。
边栏推荐
猜你喜欢
随机推荐
暴力递归到动态规划 05 (贴纸拼词)
el-table中加入el-input框和el-input-number框,实现el-table的可编辑功能
Codeforces Round #809 (Div. 2) A~D
go language study notes 3
pytorch官网中如何选择以及后面的安装和pycharm测试步骤
JVM 类加载机制 超详细学习笔记(三)
无代码开发平台重新申请入门教程
JVM 垃圾回收 超详细学习笔记(二)
力扣344-反转字符串——双指针法
An old programmer's summary review of 2020, how to become more awesome in 2021
ms project2010项目管理软件使用技巧总结
Hexagon_V65_Programmers_Reference_Manual(14)
Seata异常:endpoint format should like ip:port
pycharm上的tensorflow环境搭载
MySQL - Function and Constraint Commands
1475. 商品折扣后的最终价格
Kyligence 再获 CRN, insideBIGDATA 两大国际奖项认可
How MySQL to prepare SQL pretreatment (solve the query IN SQL pretreatment can only query out the problem of a record)
RadonDB MySQL on K8s 2.1.3 发布!
光明区关于促进科技创新的若干措施(征求意见稿)





![[High Performance Computing] openMP](/img/a5/2cfd760a26edb379d337eb3d1605d5.jpg)


