sql中union和union all
實驗,只是驗證一下union和union all的區別,很簡單。(Oracle和mysql)
- UNION 將兩個SQL語句的結果合併起來,並去重。
- union all 會顯示全部記錄。
Oracle
> create table o1 (id number(10),name varchar2(100)); Table created. > create table o2 (id number(10),name varchar2(100)); Table created. > create table o3 (id number(10),name varchar2(100)); Table created. > insert into o1 values(1,'TOM'); 1 row created. > insert into o2 values(1,'TOM'); 1 row created. > insert into o1 values(2,'ARUP'); 1 row created. > insert into o1 values(3,'JACK'); 1 row created. > commit; Commit complete. > select * from o1; ID NAME > select * from o2; ID NAME > select * from o1 ID NAME > select * from o1 ID NAME |
mysql
mysql> create table t1 (id int(10),name varchar(100));
Query OK, 0 rows affected (0.06 sec) mysql> create table t2 (id int(10),name varchar(100)); Query OK, 0 rows affected (0.04 sec) mysql> insert into t1 values(1,'TOM'); Query OK, 1 row affected (0.02 sec) mysql> insert into t2 values(1,'TOM'); Query OK, 1 row affected (0.03 sec) mysql> insert into t1 values(2,'ARUP'); Query OK, 1 row affected (0.01 sec) mysql> insert into t1 values(3,'JACK'); Query OK, 1 row affected (0.01 sec) mysql> SELECT * FROM T1 -> ; +------+------+ | id | name | +------+------+ | 1 | TOM | | 2 | ARUP | | 3 | JACK | +------+------+ 3 rows in set (0.00 sec) mysql> SELECT * FROM T2 -> ; +------+------+ | id | name | +------+------+ | 1 | TOM | +------+------+ 1 row in set (0.00 sec) mysql> select * from t1 -> union -> select * from t2; +------+------+ | id | name | +------+------+ | 1 | TOM | | 2 | ARUP | | 3 | JACK | +------+------+ 3 rows in set (0.00 sec) mysql> select * from t1 -> union all -> select * from t2; +------+------+ | id | name | +------+------+ | 1 | TOM | | 2 | ARUP | | 3 | JACK | | 1 | TOM | +------+------+ 4 rows in set (0.00 sec) |
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/23650854/viewspace-687145/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- sql中union和union all的用法SQL
- sql中UNION和UNION ALL的區別SQL
- union 和union all 使用區別
- union all和union的區別
- union和union all 關鍵字
- `FULL JOIN` 和 `UNION ALL`
- MySQL學習(五) UNION與UNION ALLMySql
- Oracle的集合操作(union、union all、intersect、minus集合函式)Oracle函式
- union all 最佳化案例
- sql注入之union注入SQL
- SQL Server中的集合運算: UNION, EXCEPT和INTERSECTSQLServer
- MYSQL學習筆記24: 多表查詢(聯合查詢,Union, Union All)MySql筆記
- union和enum使用
- SQL優化案例-union代替or(九)SQL優化
- oracle知識整理(1) union和union all的區別,left join和right join的區別(各種join的區別)Oracle
- SQL最佳化案例-union代替or(九)SQL
- union用法
- union注入
- OceanBase 金融專案最佳化案例(union all 改寫)
- 銀彈谷V平臺VSQL使用distinct與union all使用SQL
- msyql jion 和 union 的區別
- Struct 和 Union有下列區別Struct
- SQLite語句(三):JOIN和UNIONSQLite
- union 聯合體
- union存取低高位
- MySQL中union和order by同時使用的實現方法MySql
- MySQL中使用or、in與union all在查詢命令下的效率對比MySql
- 組合查詢(UNION)
- MySQL, Incorrect usage of UNION and ORDER BYMySql
- [CF1517F] Union
- 並查集(Union Find)並查集
- Union Find程式碼塊
- kingbase SQL最佳化案例 ( union遞迴 改 cte遞迴 )SQL遞迴
- C語言:一個例子理解 union 和 structC語言Struct
- Analysis of Set Union Algorithms 題解Go
- WPF Path GeometryCombineMode Union, Exclude,Intersect,Xor
- 備忘:union()後paginate分頁
- Oracle優化案例-union代替or(九)Oracle優化
- 在實際應用中聯合體union的妙用