10.17 (下午)开课一个月零十三天 (高级查询)

本文详细介绍了SQL中的连接查询、联合查询及子查询等高级查询技巧,并通过实例展示了如何使用这些查询来解决复杂的数据检索问题。

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

1.连接查询

select * from 表1,表2

形成笛卡尔积

select * from info,nation where info.nation=nation.code

select info.code,info.name,info.sex,nation.name as'要修改的列名',info.birthday from info,nation where info.nation=nation.code

select * from info join nation on info.nation=nation.code

2.联合查询

select code,name from info

union

select code,name from nation

3.子查询

子查询查询的结果作为父查询的条件

(1)无关子查询:子查询执行的时候和父查询没有关系

查民族为'汉族'的所有学生信息
select * from Info where nation=(select code from nation where name='汉族')

查询生产厂商为'一汽大众'的所有汽车信息

select * from car where brand=()

select brand_code from brand where prod_code=()

select prod_code from productor where prod_name='一汽大众'

select * from car where brand in(select brand_code from brand where prod_code=(select prod_code from productor where prod_name='一汽大众'))

(2)相关子查询:子查询在执行的时候需要用到父查询的内容

查询汽车表中,汽车油耗小于该系列平均油耗的所有汽车信息

select * from car where oil<(该系列平均油耗) select avg(oil) from car where brand =(该系列)

select * from car a where oil<(select avg(oil) from car b where b.brand =a.brand)

 

转载于:https://www.cnblogs.com/l5580/p/5969863.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值