2015-04-21 40 views
1

順序表MySQL的操作數應包含1列(S)INSERT

id 
orderlinesid 
productvaritiesid 

orderlines表

orderlinesid 
productvaritiesid(fk) 

我試圖做一個插入到一個Order表。我的查詢返回以下異常"Operand should contain 1 column(s)"。通過查看這個問題,我可以看到子查詢中只有一列被返回。

我的問題是我該如何去嘗試實現這一目標?

INSERT INTO refund (refund.orderlinesid, refund.productvaritiesid) VALUES 
((select orderlines.orderlinesid, orderlines.product_varities_id from orderlines where orderlines.order_id = 54 AND orderlines.product_varities_id = 3)); 
+1

沒有括號,沒有值,即'INSERT INTO(X,Y,Z)選擇X,Y,Z FROM ...'。 'VALUES'語法用於從輸入數據中進行單行插入,而'INSERT INTO ... SELECT'用於複製另一個查詢中的行。 – StuartLC

+0

這是有效的,但你能解釋爲什麼這個語法是正確的嗎?@StuartLC – Haris

+1

@Harry MySQL文檔有一些解釋:https://dev.mysql.com/doc/refman/5.1/en/insert-select.html – dan08

回答

0

您的代碼的語法不正確, 值是指特定的情況下

INSERT INTO refund 
    (refund.orderlinesid, refund.productvaritiesid) 
select orderlines.orderlinesid, orderlines.product_varities_id 
from orderlines 
where orderlines.order_id = 54 AND orderlines.product_varities_id = 3; 
相關問題