PostgreSQL(EXCEPT,INTERSECT)

except 能夠查看錶一對錶二不同的數據,有點像是對錶一進行表一表二交集的反集的交集,好繞;table

intersect 能夠查看錶一和表二同樣的數據,求交集;select

 

select t1.name,t1.age,t1.country from table_1 t1數據

excepttab

select t2.name,t2.age,t2.country from table_2 t2 order by name,age;co

能夠對兩表使用where條件;ab

 

能夠將except結果建立成一張新的表:

create table table_3 as (

select t1.name,t1.age,t1.country from table_1 t1

except

select t2.name,t2.age,t2.country from table_2 t2 order by name,age;

);

 

能夠將except結果插入另外一張表:

insert into table_2(name,age,country)

select t1.name,t1.age,t1.country from table_1 t1

except

select t2.name,t2.age,t2.country from table_2 t2 order by name,age;