多表sql查詢求和

2021-08-03 11:02:34 字數 1077 閱讀 4568

昨天由於需要統計某一時間段內的資料,需要兩張關聯表的資料和,

表a的sql查詢結果

select count(*)

from 表a  a

where a.task_type = 2

and a.create_time >= to_date('2017-01-30', 'yyyy-mm-dd')

and a.create_time < to_date('2017-07-07', 'yyyy-mm-dd')

表b的sql查詢結果

select count(*)

from 表b  b

where b.task_type = 2

and b.create_time >= to_date('2017-01-30', 'yyyy-mm-dd')

and b.create_time < to_date('2017-07-07', 'yyyy-mm-dd')

求兩條sql之和,使用union all函式

select count(*)

from (select *

from 表a a

where a.task_type = 2

and a.create_time >= to_date('2017-01-30', 'yyyy-mm-dd')

and a.create_time < to_date('2017-07-07', 'yyyy-mm-dd')

union all

select *

from 表b  b

where b.task_type = 2

and b.create_time >= to_date('2017-01-30', 'yyyy-mm-dd')

and b.create_time < to_date('2017-07-07', 'yyyy-mm-dd'));

此處union all

這個指令的目的也是要將兩個 sql 語句的結果合併在一起。 

union all

和union

不同之處在於

union all

會將每乙個符合條件的資料都列出來,無論資料值有無重複

sql 縱向求和 SQL 多表查詢

實際工作中,絕大部分查詢並非乙個表可以解決,我們需要合併,連線 所有查詢都其實都是在簡單查詢的基礎上進行的。一 的合併 縱向增加 的合併或者說加法,是把兩個 加在一起,這個操作增加的是行,也就是說 會邊長。假設有個兩個表如下 表 course 表 course1 經過這個union 操作後,兩個表合...

SQL 多表查詢

不同的 sql join inner join 內連線 如果表中至少有乙個匹配,也從左表返回所有的行 left join 左連線 即使右表中沒有匹配,也從右表返回所有的行 right join 右連線 即使左表中沒有匹配,也從右表返回所有的行 full join 全連線 只有其中乙個表中存在匹配也從...

SQL 多表查詢

join操作符 1.笛卡爾積,rxs 可直接轉換為sql語句 2.等值連線,記作 可直接轉換為sql語句 3.自然連線,記作 可轉換為sql語句 4.左外連線和右外連線的表示方法及轉換為sql 注意若多個關係有同名屬性,則用 關係名.屬性名 指出重名屬性 連線也可以與投影,選擇等結合使用。1.查詢選...