我有三个表,它们有四个相同的列。我想要一个从这四列中检索数据的查询。例如,四列分别是id、name、email、phone。我想从这四列中检索数据。
有人可以帮忙吗?
最佳答案
使用UNION :
select id, name, email, phone
from table1
union
select id, name, email, phone
from table2
union
select id, name, email, phone
from table3;
在上面的查询中,来自不同表的相同行将显示为一行。如果您想要所有表中的所有行,请使用 UNION ALL。
使用 INTERSECT 在所有三个表中只选择相同的行:
select id, name, email, phone
from table1
intersect
select id, name, email, phone
from table2
intersect
select id, name, email, phone
from table3;
关于sql - 使用postgresql从多个表中选择相同的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35106706/