当前位置:网站首页>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的关系分开了,分开后就可能出现结果列错位的情况,可能名字和他的分数对不上。
边栏推荐
- leetcode hot 100(刷题篇11)(231/235/237/238/292/557/240/36)offer/3/4/5
- Hexagon_V65_Programmers_Reference_Manual (10)
- 容器化 | 在 KubeSphere 中部署 MySQL 集群
- std::vector中保存指针时用法
- How can I make (a == 1 && a == 2 && a == 3) to be true?
- leetcode刷题
- [Vitis] Code implementation of ZCU102 development board PS-side control PL-side reset
- 工具 | 常用 PostgreSQL 预防数据丢失方案
- Hexagon_V65_Programmers_Reference_Manual(14)
- WeChat payment and payment callback
猜你喜欢

即刻报名|如何降低云上数据分析成本?

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

上交所行情文件解析之mktdt04

L2-025 分而治之

This article will take you through js to deal with the addition, deletion, modification and inspection of tree structure data

The Golden Circle Rule: Deep Thinking Methods for Successful People

ugly programmer

el-table中加入el-input框和el-input-number框,实现el-table的可编辑功能

从想当亿万富翁到职场、创业、爱情、抑郁、学医学武,我的程序人生
随机推荐
Hexagon_V65_Programmers_Reference_Manual(14)
mysql cannot connect remotely Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (10060 "Unknown error")
leetcode hot 100(刷题篇11)(231/235/237/238/292/557/240/36)offer/3/4/5
(Hexagon_V65_Programmers_Reference_Manual(13)
力扣05-替换空格——字符串问题
IIS网站出现401未授权访问
MySQL夺命10问,你能坚持到第几问?
无代码开发平台子管理员入门教程
This article will take you through js to deal with the addition, deletion, modification and inspection of tree structure data
DLL description (1)
面试题 17.13. 恢复空格(字典树)
How MySQL to prepare SQL pretreatment (solve the query IN SQL pretreatment can only query out the problem of a record)
GO language study notes one
程序员赚钱实操,手把手教你做付费课程,自媒体,付费文章及付费技术课赚钱
Record of problems encountered by the pyinstaller packager
程序员大保健指南,给自己的身心偶尔放松的机会
从驱动表和被驱动表来快速理解MySQL中的内连接和外连接
oracle触发器的自治事务
Divide and conquer. L2-025
从字节码角度带你彻底理解异常中catch,return和finally,再也不用死记硬背了