为什么以下SQL查询包含来自两个表的结果,即A和B.

时间:2017-11-02 11:48:13

标签: sql

SELECT *
FROM (SELECT 1 AS A, 2 AS B) A 
JOIN (SELECT 1 AS C, 4 AS D) B ON A.A = B.C

输出: -

A B C D
1 2 1 4

1 个答案:

答案 0 :(得分:1)

这是因为你选择了“*”。 这意味着你想拥有A和B的所有东西。

如果您只想选择“OR”B中的列,则查询应如下所示:

SELECT A.*
FROM (SELECT 1 AS A, 2 AS B) A JOIN
     (SELECT 1 AS C, 4 AS D) B
      ON A.A = B.C

OR

SELECT B.*
FROM (SELECT 1 AS A, 2 AS B) A JOIN
 (SELECT 1 AS C, 4 AS D) B
  ON A.A = B.C