0

它可能看起来很基本,但我还没有遇到过,也无法弄清楚。假设我有两个用户表,每个表中都有一个字段“email_address”。如何从两个表中选择所有电子邮件地址作为一个列表?我很可能需要 DISTINCT 关键字,因为两个表中可能会出现相同的电子邮件地址。

我敢打赌,这是非常明显的,我只是看不出其中的逻辑。

4

5 回答 5

3
select email_address from table1
union
select email_address from table2

UNION将合并相同的电子邮件地址。

如果您想保留重复项,请使用UNION ALL

于 2013-09-10T17:00:06.540 回答
1

只有UNION两张表:

SELECT `email_address`
FROM `a`
UNION
SELECT `email_address`
FROM `b`
于 2013-09-10T16:59:54.643 回答
0
select EMAIL_NAME from table1
UNION
select EMAIL_NAME from table2
于 2013-09-10T17:00:31.223 回答
0

使用联合:

SELECT email_address FROM table1
UNION
SELECT email_address FROM table2
于 2013-09-10T17:01:30.013 回答
0

使用联合,如果您想知道哪个来自哪个表,请将表名的文字放入结果集中。

select email_name, 'table1' as table_name from table1
union
select email_name, 'table2' as table_name from table2
于 2013-09-10T17:02:47.787 回答