SELECT链接MYSQL上的两个查询的结果

时间:2012-02-17 10:02:35

标签: mysql sql database select

我想链接两个查询的内容。请注意我不想使用CONCAT()CONCAT_WS(),因为这不是我想要实现的目标。

查询1结果:

Id    Name
1     John
2     Mike

查询2结果:

Id    Name
3     Emily
7     Robert

我想要的是:

Id    Name
1     John
2     Mike
3     Emily
7     Robert

我也可以将两个查询重写为一个。那太好了。以下是我目前失败的尝试:

SELECT a.x AS x, a.y AS y FROM a;
SELECT b.x AS x, b.y AS y FROM b;

谢谢。

5 个答案:

答案 0 :(得分:4)

使用UNION ALL将多个查询连接到单个结果集:

SELECT a.x AS x, a.y AS y FROM a
UNION ALL
SELECT b.x AS x, b.y AS y FROM b

注意

UNION ALL将保留所有裁员。请阅读手册页并修改查询以达到您想要的行为。

答案 1 :(得分:1)

SELECT a.x AS x, a.y AS y FROM a
union all
SELECT b.x AS x, b.y AS y FROM b;

答案 2 :(得分:1)

使用UNION

SELECT a.x AS x, a.y AS y FROM a
UNION ALL
SELECT b.x AS x, b.y AS y FROM b;

答案 3 :(得分:1)

如何使用union?

SELECT a.x AS x, a.y AS y 
FROM a
UNION
SELECT b.x AS x, b.y AS y 
FROM b;

答案 4 :(得分:0)

你似乎想要一个工会:

SELECT a.x AS x, a.y AS y FROM a 
UNION
SELECT b.x AS x, b.y AS y FROM b

如果您想要保留可能的冗余,请使用UNION ALL。