mysql 子查询种类及联合查询的sql语句写法

本文深入解析了SQL中的子查询与联合查询技术,包括in、not in、比较运算符、exists、any|some all等子查询用法,以及union和union all的联合查询技巧。通过具体实例展示了如何使用这些高级查询语句来处理复杂的数据检索需求。

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

子查询:

将一个查询语句嵌套在另一个查询语句中,内层查询语句的查询结果可以作为外层查询语句提供条件。

1.in ,not in

2.比较运算符>=

select id,name from student where score >=(select level from scholarship where id=1)

3.[not] exists

select  id,username from employee where exists(select id   from department where id=5)

4.any|some  all配合>=比较运算符

select id,name from student where score >=any(select level from scholarship)

5.将查询结果写入数据表   保证字段顺序,字段数目一致test1的列得存在

insert test1(id,num) select id,score from student;

6.创建数据表同时将查询结果写入到数据表   如果字段名称不一致,以查询出来的为主,原字段为null

create table [if not exists] employee(id,score) select id,score from student;

 

联合查询:

union去掉重复的 union all简单并集   两张表的查询的列数量一样,类型可以不一样

select id from bbs_addr union select name from bbs_brand;  id是BigInt  name是varchar类型

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值