从联接中选择不同的值会导致Mysql

时间:2018-03-08 19:20:40

标签: mysql sql

我正在尝试从mysql中的两个表中访问完整数据,然后从结果集中查找列的不同值。尝试使用嵌套查询,如下所示: -

Select distinct s.BlockName 
from (SELECT * 
      FROM constucted
      LEFT JOIN required ON constucted.BlockName = required.BlockName 

      UNION

      SELECT * 
      FROM constucted
      RIGHT JOIN required ON constucted.BlockName = required.BlockName
     ) s

Stackoverflow Reference中所述 并尝试使用with关键字如下: -

WITH CTE
AS
(
   SELECT * 
   FROM constucted
   LEFT JOIN required ON constucted.BlockName = required.BlockName

   UNION

   SELECT * 
   FROM constucted
   RIGHT JOIN required ON constucted.BlockName = required.BlockName
)
SELECT DISTINCT BlockName
FROM CTE

但无法找到BlockName列无法为其编写查询的明确值。

1 个答案:

答案 0 :(得分:1)

您的查询似乎更简单地写为:

select BlockName 
from constructed
union -- intentional to remove duplicates
select BlockName
from required;