今天工作中遇到的需求,要查出幾個表中的數據,但是本着方便客戶的思想,想將這幾條查詢語句變成一條,只需要一次就可以查詢到所有的結果,妹滴狠。
1. 使用子查詢以及union all,拼接查詢結果
1.1 union all 解析
union all,用於合併兩個或多個select語句的結果集。需注意,列的數量必須相同,列名不必相同,列的類型也不必相同,如下:
--建表
Create table Table1
(
id int not null,
name varchar(20) not null
)
Create table Table2
(
id int not null,
name varchar(20) not null
)
--插入數據
Insert into Table1 values (1,'姚羽')
Insert into Table1 values (2,'邊兵兵')
Insert into Table1 values (3,'袁磊')
Insert into Table2 values (1,'姚羽')
Insert into Table2 values (2,'柳春平')
Insert into Table2 values (3,'張永超')
Insert into Table2 values (4,'劉華健')
--執行sql
--select1
select id,name from Table1
union all select id,name from Table2;
--select 2
select id id1,name from Table1
union all select id,name from Table2;
--select3
select id,name from Table1
union all select name,id from Table2;
以下分別爲執行結果:
select1: select2:select3:
補充:形象的展示以下union 和 union all 的區別吧:
--select4
select id,name from Table1
union select id,name from Table2;
運行結果如下:
大概形象看看出來,union會去除重複行。
2.分次查詢
--要將沒有關聯的表的結果集合在一起 就可以執行以下代碼
--表名以及字段名保密
--select1
select count(distinct column1) AS total
from table1 t join table2 f
on t.ZJLX = f.GLOBAL_TYPE and t.ZJHM=f.GLOBAL_ID
WHERE f.column2='000' and f.column3='B0000' and t.column4='000';
--select2
SELECT COUNT(distinct column1) AS total1
FROM table2
where column2='TAX' and column2='01';
--select3
SELECT sum(distinct column1) AS total2
FROM table2
where column2='TAX' and column2='01';
--直接union all 需要將他們的字段區分開來
--select4
select count(distinct column1) AS total ,0 AS total1, 0 AS total2
from table1 t join table2 f
on t.ZJLX = f.GLOBAL_TYPE and t.ZJHM=f.GLOBAL_ID
WHERE f.column2='000' and f.column3='B0000' and t.column4='000';
--select5
SELECT 0 AS total,COUNT(distinct column1) AS total1 ,0 AS totak2
FROM table2
where column2='TAX' and column2='01';
--select6
SELECT 0 AS total,0 AS total1,sum(distinct column1) AS total2
FROM table2
where column2='TAX' and column2='01';
--這樣保證每個想查的結果都可以顯示出來,並且列數量相同,可以拼接
--select7
select total,total1,total2
from (select count(distinct column1) AS total ,0 AS total1, 0 AS total2
from table1 t join table2 f
on t.ZJLX = f.GLOBAL_TYPE and t.ZJHM=f.GLOBAL_ID
WHERE f.column2='000' and f.column3='B0000' and t.column4='000'
union all SELECT 0 AS total,COUNT(distinct column1) AS total1 ,0 AS totak2
FROM table2
where column2='TAX' and column2='01'
union all SELECT 0 AS total,0 AS total1,sum(distinct column1) AS total2
FROM table2
where column2='TAX' and column2='01')
--但是這樣下來發現三行三列,不美觀,這時候將其餘兩行賦值爲0的作用就顯現出來了
--select8
select sum(total),sum(total1),sum(total2)
from (select count(distinct column1) AS total ,0 AS total1, 0 AS total2
from table1 t join table2 f
on t.ZJLX = f.GLOBAL_TYPE and t.ZJHM=f.GLOBAL_ID
WHERE f.column2='000' and f.column3='B0000' and t.column4='000'
union all SELECT 0 AS total,COUNT(distinct column1) AS total1 ,0 AS totak2
FROM table2
where column2='TAX' and column2='01'
union all SELECT 0 AS total,0 AS total1,sum(distinct column1) AS total2
FROM table2
where column2='TAX' and column2='01')
運行結果如下:
select1: select2: select3:
select7:
select8: