将SELECT查询组合在两个没有连接的表上

时间:2014-09-08 12:28:09

标签: mysql

是否可以在两个完全没有连接的表上运行组合的SELECT查询?

**Table A**         **Table B***
id | A | B | C      id | D | E | F
---------------     ---------------
X1 | 1 | 2 | 3      Y1 | 1 | 2 | 3
---------------     ---------------
X2 | 1 | 2 | 3      Y2 | 1 | 2 | 3
---------------     ---------------
X3 | 1 | 2 | 3      Y3 | 1 | 2 | 3

在两个查询中,它将是:

   SELECT A,B FROM Table A WHERE id=X2
   SELECT D,E FROM Table B WHERE id=Y1

如果可能的话,我想把这两者结合起来像

SELECT (A,B FROM Table A WHERE id=X2) AND (D,E FROM Table B WHERE id=Y1)

只有一个结果集。

1 个答案:

答案 0 :(得分:1)

使用UNION

(SELECT A as col1 ,B as col2 FROM Table A WHERE id=X2) union (SELECT D as col1,E as col2 FROM Table B WHERE id=Y1)