1

我一直在寻找解决我的问题的方法,但找不到任何地方。这是我的问题:我有来自两个不同表的两列,并希望将它们插入另一个空表。我试图运行这个查询,但它不起作用,我什至不知道为什么:

SELECT a.column, c.column 
FROM FirstTable.column a, SecondTable.column c
left outer join ThirdTable.column b on a.column = b.column
left outer join ThirdTable.column b on c.column = b.column

运行此程序后,我收到以下消息:

Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "a.column" could not be bound.
Msg 1011, Level 16, State 1, Line 1
The correlation name 'b' is specified multiple times in a FROM clause.

我还要补充一点,a.column 有 1143 行,c.column 有 2057 行。两者都是各自表的PK。

任何帮助将不胜感激。

4

3 回答 3

3

Try reframing your query as

SELECT a.column, c.column 
FROM Table a left outer join Table b on a.column = b.column 
left outer join Table c on c.column = b.column

Would have been better if you provided exact query, but the problem is most likely with the joins.

于 2013-08-08T20:35:11.040 回答
1

我使用了一组临时表变量来显示这个概念,但它应该很容易转换为实际的表。

DECLARE @tabl1 TABLE (col1 int DEFAULT 1)
    INSERT INTO @tabl1 VALUES (1)

DECLARE @tabl2 TABLE (col2 int DEFAULT 2)
    INSERT INTO @tabl2 VALUES (2)

DECLARE @tabl3 TABLE (col1 int, col2 int)

-- This should be what you need.

INSERT INTO @tabl3
SELECT a.col1
     , b.col2
FROM @tabl1 a, @tabl2 b

SELECT * FROM @tabl3
于 2013-08-08T20:38:25.190 回答
0

除非您进行笛卡尔连接,否则您将混合使用显式和隐式语法。我会选择其中一个(我自己偏爱显式语法):

SELECT a.column, c.column 
FROM Table a
left outer join Table b on a.column = b.column
left outer join Table c on b.column = c.column

或任何适合您的结构的东西。

于 2013-08-08T20:32:25.987 回答