mysql 使用子查询_MySQL- 子查询的使用

MySQL

子查询的使用

什么是子查询

子查询是将一个 SELECT 语句的查询结果作为中间结果,供另一个 SQL 语句调用。

像这样:

-- 我们将学生表中的所有班级ID当做中间结果

select *from t_class where c_id in (select distinct c_id from t_student);

常用比较符

子查询最常用的用法:

non_subquery_operand comparison_operator (subquery)

其中操作符通常为

= > < >= <= <> != <=>

其他的都不说了,这里说下这个<=>,以前还真没用过

<=>和=比较类似,也是判断是否相等,相等返回1,不相等返回2

mysql> select 1<=>1,1<=>2;

+-------+-------+

| 1<=>1 | 1<=>2 |

+-------+-------+

| 1 | 0 |

+-------+-------+

1 row in set

mysql> select 1=1,1=2;

+-----+-----+

| 1=1 | 1=2 |

+-----+-----+

| 1 | 0 |

+-----+-----+

1 row in set

和=不一样的地方,是对NULL的支持,用<=>可以判断是否为null,而等号则是出现null,结果就为null

mysql> select 1<=>null,null<=>null,1=null,null=null;

+----------+-------------+--------+-----------+

| 1<=>null | null<=>null | 1=null | null=null |

+----------+-------------+--------+-----------+

| 0 | 1 | NULL | NULL |

+----------+-------------+--------+-----------+

1 row in set

any、in、some

在子查询中,in平时用的比较多,这个any、some,这里简单说下any和some

operand comparison_operator ANY (subquery)

operand comparison_operator SOME (subquery)

comparison_operator 可以为 = > < >= <= <> !=

any表示任意一个值,比如

> any () :表示大于any中任意一个值,即>子查询中最小值

< any(): 表示小于any中任意一个值,即

= any(): 和in一样

<> any():比较好玩儿,如果子查询返回多个值,<> any会返回所有值

select *from t_student

where s_id > any(

select s_id from t_student where s_id in (105,109,111)

);

a8fe5ab6be4a

>any

select *from t_student

where s_id = any(

select s_id from t_student where s_id in (105,109,111)

);

a8fe5ab6be4a

=any

some 和any是一样的就不多说了

all

这个all我也没咋用过,all表示所有值,和any有点儿相反的意思

operand comparison_operator ALL (subquery)

> all ():表示大于所有值,即>子查询中最大值

< all() : 表示小于所有值,即< 子查询中的最小值

= all(): 返回单个值时和=一样,返回多个值时貌似没啥用

<> all(): 和not in 一样

select *from t_student

where s_id > all(

select s_id from t_student where s_id in (105,109)

);

a8fe5ab6be4a

>all

标量子查询

这种情况下,子查询返回单个值,可以在任何地方使用它。

select

c_id,

c_name,

(select max(s_id) from t_student) as max_s_id

from

t_class;

select

*

from

t_class

where

c_id = (select max(c_id) from t_class);

行子查询

上面我们介绍的子查询,都是返回1列多行,行子查询的话,是返回1行多列

-- 查询一班所有男生

select *from t_student

where (c_id,s_gender) = (901,0);

a8fe5ab6be4a

行子查询

这里也可以返回多行多列(也叫做表子查询)

select *from t_student

where (c_id,s_gender) in (select 901,0 union select 902,0);

a8fe5ab6be4a

多行多列

参考资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值