UNION
語義是重複被刪除。 PostgreSQL使用Hash函數來刪除重複項,結果按鍵的哈希順序排序。
您可以使用UNION ALL
,但SQL仍然不保證訂單,除非您使用ORDER BY
子句。
EXPLAIN
SELECT 'TEST1'
UNION SELECT 'TEST2'
UNION SELECT 'TEST3'
產地:
HashAggregate (cost=0.07..0.10 rows=3 width=0)
-> Append (cost=0.00..0.06 rows=3 width=0)
-> Subquery Scan on "*SELECT* 1" (cost=0.00..0.02 rows=1 width=0)
-> Result (cost=0.00..0.01 rows=1 width=0)
-> Subquery Scan on "*SELECT* 2" (cost=0.00..0.02 rows=1 width=0)
-> Result (cost=0.00..0.01 rows=1 width=0)
-> Subquery Scan on "*SELECT* 3" (cost=0.00..0.02 rows=1 width=0)
-> Result (cost=0.00..0.01 rows=1 width=0)
而
EXPLAIN
SELECT 'TEST1'
UNION ALL SELECT 'TEST2'
UNION ALL SELECT 'TEST3'
產地:
Append (cost=0.00..0.06 rows=3 width=0)
-> Subquery Scan on "*SELECT* 1" (cost=0.00..0.02 rows=1 width=0)
-> Result (cost=0.00..0.01 rows=1 width=0)
-> Subquery Scan on "*SELECT* 2" (cost=0.00..0.02 rows=1 width=0)
-> Result (cost=0.00..0.01 rows=1 width=0)
-> Subquery Scan on "*SELECT* 3" (cost=0.00..0.02 rows=1 width=0)
-> Result (cost=0.00..0.01 rows=1 width=0)
僅供參考 - MS SQL 08返回1,2,3,我所期待。 – asawyer 2011-02-17 19:30:25
瘋狂的猜測,嘗試`聯合所有`` – 2011-02-17 19:31:15