合并多個查詢結果
更新時間 2025-02-14 10:21:41
最近更新時間: 2025-02-14 10:21:41
分享文章
本文為你介紹合并多個查詢結果的方法。
不過濾重復的記錄。
teledb=# create table teledb_pg1(id int, nickname varchar); CREATE TABLE teledb=# insert into teledb_pg1 values(3, 'pg'),(5,'test'); COPY 2 teledb=# select * from teledb_pg union all select * from teledb_pg1; id | nickname ----+------------ 1 | teledb 1 | hello,pgxc 2 | TELEDB 3 | pg 4 | 5 | test 3 | pg (7 rows)過濾重復的記錄。
teledb=# select * from teledb_pg union select * from teledb_pg1; id | nickname ----+------------ 1 | teledb 1 | hello,pgxc 4 | 5 | test 2 | TELEDB 3 | pg (6 rows)每個子查詢分布在合并結果中的使用。
teledb=# select * from (select * from teledb_pg limit 2) as t union all select * from (select * from teledb_pg1 limit 2) teledb-# ; id | nickname ----+------------ 1 | teledb 1 | hello,pgxc 5 | test 3 | pg (4 rows)