关于union和子查询过程中的给表起别名(个人笔记)

您所在的位置:网站首页 access怎么给表重命名 关于union和子查询过程中的给表起别名(个人笔记)

关于union和子查询过程中的给表起别名(个人笔记)

2024-07-12 16:23| 来源: 网络整理| 查看: 265

一段语句不能运行:

select status,count(*) from (select invoice_id,vendor_id,invoice_total,'1st' as status from invoices where invoice_total > 1000 union select invoice_id,vendor_id,invoice_total,'2nd' as status from invoices where invoice_total between 500 and 1000 union select invoice_id,vendor_id,invoice_total,'3rd' as status from invoices where invoice_total 1000 union select invoice_id,vendor_id,invoice_total,'2nd' as status from invoices where invoice_total between 500 and 1000 union select invoice_id,vendor_id,invoice_total,'3rd' as status from invoices where invoice_total 1000 union select invoice_id,vendor_id,invoice_total,'2nd' as status from invoices where invoice_total between 500 and 1000 union select invoice_id,vendor_id,invoice_total,'3rd' as status from invoices where invoice_total (select max(sal) from emp where deptno = 30)

子查询无法使用的情况:

1.group by后面不能使用子查询

2.单行子查询使用单行操作符,多行子查询使用多行操作符

        单行:运算符等,

        多行:in(等于列表中的任意一个) any(和子查询返回的任意一个值比较) all(和子查询返回的所有值比较)

3.子查询中的空值问题

是:where empno in (select mgr from emp) 

否:where empno not in (select mgr from emp where mgr is not null)   如果用not in查询,要注意空值,否则可能返回0行

拓展

子查询可以和多表查询相互代替

TOP-N问题中的伪列:rownum(只能在Oracle中使用)



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3