SQL - 子查询
时间:2025/7/11 10:25:19来源:https://blog.csdn.net/m0_74403543/article/details/141299505 浏览次数:0次
- 子查询可以写的位置:select、from、where
- 子查询可以返回一个值:一个列的一些值,一个结果集(表)
- 子查询的作用:可以作为条件判断的范围,作为判断条件,可以返回特定结果值,
- 与子查询相关:
- 相关子查询
- 子查询和外查询存在相关性,子查询的返回结果集和外查询存在联系,但是相关子查询经常执行得很慢
-
select *
from employees e
where salary >(
select avg(salary)
from employees
where office_id=e.office_id)
-- 也可以写这样,但是体现不了相关子查询的特点
-- select *
-- from employees
-- join (select office_id,avg(salary) as aaa
-- from employees
-- group by office_id) o
-- on o.office_id=employees.office_id
-- where salary>aaa
- select 子句中的子查询
-
select invoice_id,invoice_total,(select avg(invoice_total) from invoices) as invoice_avg,invoice_total- (select invoice_avg) as defference
from invoices;select client_id,name,sum(invoice_total),
(select avg(invoice_total) from invoices) as average,
sum(invoice_total)-(select average) as difference
from clients
left join invoices using (client_id)
group by client_id,name
关键字:SQL - 子查询
版权声明:
本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。
我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com
责任编辑: