sql如何同時查詢多張表(union關鍵字)

2021-08-07 05:45:35 字數 1004 閱讀 6514

1、例子1

select * from a union all 

select * from b union all 

select * from c 

如果a,b,c裡有相同項的話會一併顯示出來

select * from a union 

select * from b union 

select * from c 

這樣的做法是如果三張表有完全相同的項,會只顯示一條

2、例子2

sql union 操作符 

union 操作符用於合併兩個或多個 select 語句的結果集。 

請注意,union 內部的 select 語句必須擁有相同數量的列。列也必須擁有相似的資料型別。同時,每條 select 語句中的列的順序必須相同。 

sql union 語法

select column_name(s) from table_name1 

union 

select column_name(s) from table_name2

注釋:預設地,union 操作符選取不同的值。如果允許重複的值,請使用 union all。 

sql union all 語法 

select column_name(s) from table_name1 

union all 

select column_name(s) from table_name2 

另外,union 結果集中的列名總是等於 union 中第乙個 select 語句中的列名。

3、例子3

估計樓主是想合併三張表中的查詢結果: 

select * from a where

union 

select * from b where

union 

select * from c where

shell指令碼同時刪除多張表

同時刪除表名類似的多張表 引數1是資料庫名 引數2傳表名 bin sh if ne 2 then echo input argument dbname universaltablename exit 1 fimysqlconnect mysql droptable mysqlconnect e se...

Linq 查詢多張表,關係表

專案中遇到乙個問題,有4張表,然後相互之間有3張關係表關聯,一共七張表。想要從頂層表查詢最底層表的記錄,不能寫7層巢狀。用linq實現特別簡單,表 user,role,module,function以及user role,role module,module function,var fs from...

Dapper同時操作任意多張表的實現

using microsoft.extensions.configuration using system using system.collections.generic using system.text namespace feg.esb.data.ef catch exception 查詢 ...