mysql数据查询之子查询

本文详细介绍了SQL中的子查询概念及应用,包括标量子查询、列子查询、行子查询、表子查询和exists子查询五种类型,并通过具体示例说明了每种类型的使用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

子查询概念:
sub query查询是在某个查询结果之上进行的,一条select语句内部包含了另外一条select语句。

(1)标量子查询 where 之后写 = ,确定某一个值
select * from student where c_id = (select id from class where grade = "PM3.1");

(2)列子查询    where 之后 写 in,是一列的所有值
select * from student where c_id in (select id from class);

(3)行子查询    两点确定一条直线 ,where之后放两个值,或者直接用max() 或 min()
    • select * from student where age = ? and height = ?;
  • 然后确定最大年龄和最大身高。 

    • select max(age), max(height) from student;


(4)表子查询比较特殊,查询内容放在from之后,查询得到一个表,最好再有个别名
-- 表子查询
select * from (select * from student order by height desc) as student group by c_id;


(5)exists子查询,放在where之后

select * from student where exists(select * from class);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值