2017-01-25 52 views
0

該查詢返回多標識不能開往INSERT INTO選擇查詢,多標識符無法綁定

table2.AId and table1.LId = table2.LId 

查詢是

insert into table1(col1) 
select col2 from table2 
where table1.AId = table2.AId and table1.LId = table2.LId; 

你的幫助是高度讚賞

+0

查詢應該怎麼辦? –

+0

要在選擇查詢中使用table1列,必須加入table1。 –

回答

2

您需要在FROM條款中包含SELECT中的所有表格。所以,這是語法正確:

insert into table1(col1) 
    select t2.col2 
    from table2 t2 join 
     table1 t1 
     on t1.AId = t2.AId and t1.LId = t2.LId; 

不過,你可能打算UPDATE

update t1 
    set col1 = t2.col2 
    from table1 t1 join 
     table2 t2    
     on t1.AId = t2.AId and t1.LId = t2.LId; 

這種修改在t1行。 insert的版本將只在一列中插入具有新值的新行。

+0

對我來說太快了:-) –

+0

我錯過了加入。謝謝 – Binto

1

你的選擇查詢不「知道」,在表中插入到的一部分,所以你應該使用一個連接:

insert into table1(col1) 
select col2 
from table2 
inner join table1 on table1.AId = table2.AId and table1.LId = table2.LId; 

不過,我懷疑你正在尋找更新而不是插入:

update t1 
set col1 = t2.col2 
from table1 t1 
inner join table2 t2 on t1.AId = t2.AId and t1.LId = t2.LId; 
+0

我錯過了加入。謝謝 – Binto

1

缺少join條件

insert into table1(col1) 
    select t2.col2 
    from table2 t2 join 
     table1 t1 
     on t1.AId = t2.AId and t1.LId = t2.LId; 
+0

我錯過了加入。謝謝 – Binto