SQL UNION 操作符的應用

2021-09-26 19:38:54 字數 3335 閱讀 7333

union操作符用於合併兩個或多個select語句的結果集。但是要注意的是union連線的前後select語句查詢的字段要一致。

union是取不同的值,也就是不能重複的值。

2.union all

union all 可以取重複的值。

第乙個例子是查詢所有的字段,例子如下:

select

a.id,

a.in_out_flag,

a.document_number,

a.document_date,

a.warehouse_id,

a.bill_type,

a.storeman_id,

a.purpose,

a.illustrate,

a.type,

a.counterparty_code,

a.bill_number,

a.agency_department,

a.operator,

a.create_by,

a.create_time,

a.update_by,

a.update_time,

a.remark,

a.del_flag,

a.confirm_flag,

a.project_number,

a.order_no,

k.username as "storemanname",

s3.user_name as "operatorname",

d.dept_name as "agencydepartmentname",

w.warehouse_name as "warehousename",

w.warehouse_code as "warehousecode"

from

(select

*from

inv_bill

union all

select

*from

inv_bill_out

) aleft join inv_warehouse_keeper k on a.storeman_id = k.id

left join sys_user s3 on a.operator = s3.user_id

left join sys_dept d on a.agency_department = d.dept_id

left join inv_warehouse_def w on a.warehouse_id = w.id

where

a.confirm_flag = '1'

and a.del_flag = '0'

第二例子根據想要的條件進行返回結果集,例子如下:

select

a.id as "id",

a.in_out_flag as "inoutflag",

a.document_number as "documentnumber",

a.document_date as "documentdate",

a.bill_type as "billtype",

a.username as "storemanname",

a.quantity as "quantity",

a.user_name as "operatorname",

a.counterparty_code as "counterpartycode",

a.material_code as "materialcode",

a.material_name as "materialname",

a.confirm_flag as "confirmflag",

a.del_flag as "delflag",

a.warehouse_name as "warehousename",

a.warehouse_code as "warehousecode"

from

(select

a.id,

a.in_out_flag,

a.document_number,

a.document_date,

a.bill_type,

k.username,

d.quantity,

s3.user_name,

a.counterparty_code,

d.material_code,

d.material_name,

a.confirm_flag,

a.del_flag,

w.warehouse_name,

w.warehouse_code

from

inv_bill_detail d

left join inv_bill a on a.id = d.warehouse_bill_id

left join inv_warehouse_keeper k on a.storeman_id = k.id

left join sys_user s3 on a.operator = s3.user_id

left join inv_warehouse_def w on a.warehouse_id = w.id union all

select

a.id,

a.in_out_flag,

a.document_number,

a.document_date,

a.bill_type,

k.username,

d.quantity,

s3.user_name,

a.counterparty_code,

d.material_code,

d.material_name,

a.confirm_flag,

a.del_flag,

w.warehouse_name,

w.warehouse_code

from

inv_bill_detail_out d

left join inv_bill_out a on a.id = d.warehouse_bill_id

left join inv_warehouse_keeper k on a.storeman_id = k.id

left join sys_user s3 on a.operator = s3.user_id

left join inv_warehouse_def w on a.warehouse_id = w.id

) awhere

a.confirm_flag = '1'

and a.del_flag = '0'

SQL UNION 和 UNION ALL 操作符

union 操作符用於合併兩個或多個 select 語句的結果集。請注意,union 內部的 select 語句必須擁有相同數量的列。列也必須擁有相似的資料型別。同時,每條 select 語句中的列的順序必須相同。select column name s from table name1 union...

SQL UNION 和UNION ALL 操作符

union 操作符用於合併兩個或多個 select 語句的結果集。請注意,union 內部的 select 語句必須擁有相同數量的列。列也必須擁有相似的資料型別。同時,每條 select 語句中的列的順序必須相同。select column name s from table name1 union...

SQL UNION 和 UNION ALL 操作符

sql union 操作符 union 操作符用於合併兩個或多個 select 語句的結果集。請注意,union 內部的 select 語句必須擁有相同數量的列。列也必須擁有相似的資料型別。同時,每條 select 語句中的列的順序必須相同。sql union 語法 select column na...