带有join语句的子查询

时间:2021-05-11 19:24:04

标签: sql sql-server tsql

我不明白为什么它不允许我加入子查询。

请考虑以下代码段:

SELECT c.num_id
FROM (
  SELECT * 
    FROM staff_salary       as a
    FULL JOIN staff_profile as b
      ON a.num_id = b.num_id
) as c

我收到错误:

<块引用>

为“c”多次指定了“num_id”列。

为什么会这样?当我在没有 join 语句的情况下运行它时,子查询完美运行。

3 个答案:

答案 0 :(得分:1)

字段 num_id 存在于 staff_salary 和 staff_profile 中。 当你用*选择时,sql无法知道你是否引用了staff_salary.num_id o staff_profile.num_id

您必须将 * 替换为所需字段列表,例如:

SELECT c.num_id FROM (
  SELECT a.num_id 
    FROM staff_salary       as a
    FULL JOIN staff_profile as b
      ON a.num_id = b.num_id
) as c

答案 1 :(得分:0)

你想做什么?如果您想要任一表中的所有 num_id,请使用 union:

select num_id
from staff_salary
union     -- on purpose to remove duplicates
select num_id
from staff_profile;

答案 2 :(得分:0)

在内部查询中使用特定列。 <script src="https://cdn.jsdelivr.net/npm/p5@1.3.1/lib/p5.js"></script> 正在引发异常。

Wild (*)