当前位置:网站首页>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的关系分开了,分开后就可能出现结果列错位的情况,可能名字和他的分数对不上。
边栏推荐
- 剑指offer(刷题篇12)
- 力扣344-反转字符串——双指针法
- 1315_Use the LOOPBACK simulation mode to test whether the pyserial installation is successful
- pycharm上的tensorflow环境搭载
- 关于组织开展2022年广东省技术先进型服务企业认定工作的通知
- 从想当亿万富翁到职场、创业、爱情、抑郁、学医学武,我的程序人生
- VisualStudio2022 local debugging entry is particularly slow problem solving
- SQL连接表(内连接、左连接、右连接、交叉连接、全外连接)
- go语言学习笔记三
- 429. N 叉树的层序遍历(两种解法)
猜你喜欢

视野 | KeyDB:为 Web 应用而生的高性能 Redis 分支

The use of Conluce, an online document management system

MySQL如何对SQL做prepare预处理(解决IN查询SQL预处理仅能查询出一条记录的问题)

无代码开发平台子管理员入门教程

MySQL - Function and Constraint Commands
![[Redis Master Cultivation Road] Jedis - the basic use of Jedis](/img/e3/0c6efd03432a01f857796f0bf648ef.png)
[Redis Master Cultivation Road] Jedis - the basic use of Jedis

mysql基础(4)

Usage when saving pointers in std::vector

MySQL安装配置教程(超级详细)

I went to meet some successful people worth tens of millions on May 1st, and I have some new ideas and inspirations
随机推荐
[Vitis] Code implementation of ZCU102 development board PS-side control PL-side reset
go language study notes 4
C language implements highly secure game archives and reads files
ms project2010项目管理软件使用技巧总结
MySql字符串拆分实现split功能(字段分割转列、转行)
容器化 | 在 K8s 上部署 RadonDB MySQL Operator 和集群
el-table中加入el-input框和el-input-number框,实现el-table的可编辑功能
Golang channel implementation principle
(Hexagon_V65_Programmers_Reference_Manual(13)
Nuxt3 learning
Hexagon_V65_Programmers_Reference_Manual (12)
ThinkPHP high imitation blue play cloud network disk system source code / docking easy payment system program
剑指offer(刷题篇12)
Unity stepping on the pit record - the use of GetComponent
工具 | 常用 PostgreSQL 预防数据丢失方案
Kyligence 亮相第五届南方信息大会并获评“CIO 优选数字化服务商”
75. 颜色分类
工作效率-十五分钟让你快速学习Markdown语法到精通排版实践备忘
RadonDB MySQL on K8s 2.1.4 发布!
Recursive Optimization of Fibonacci Sequences "Memo Recursion"