sql - 使用postgresql从多个表中选择相同的字段

标签 sql postgresql

我有三个表,它们有四个相同的列。我想要一个从这四列中检索数据的查询。例如,四列分别是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/

相关文章:

sql - 物化 View 使用 HAVING 子句快速刷新?

sql - 日历的 Postgres 查询

node.js - Sequelize 多对多关联 : getter/setter methods do not exist

java - 升级到 postgres jdbc 驱动程序 42.2.10+ 时套接字超时不起作用

sql - 返回非限定列名的 SELECT 语句

sql - GroupBy 操作的渐近复杂度是多少?

mysql - #1062 - key 1 的重复条目 '1' - 未找到重复条目

mysql 选择加入第二行

SQL 从多个查询中返回匹配项

linux - 远程 Postgresql 端口 5400 监听但关闭?