2010-10-11 81 views
3

好吧,這個和我最後一個很相似,但我不明白...!如何使用Select-Statements將值插入到MYSQL表中

我嘗試以下操作:

Insert into table b 
    (Select column_1 from table_a where ID = 1), 
    (Select column_2 from table_a where ID = 1), 
    0, 
    (Select column_3 from table_a where ID = 1); 

但我總是得到一個語法錯誤...! 我認爲這是很合乎邏輯的,我正在努力做。

來自德國的Greetz和thx爲您解答!

回答

9

非常接近 - 使用:

INSERT INTO TABLE_B 
SELECT column_1, column_2, column_3 
    FROM TABLE_A 
WHERE id = 1 

..assuming有在TABLE_B只有三列。否則,指定正在插入的列到:

INSERT INTO TABLE_B 
    (column_1, column_2, column_3) 
SELECT column_1, column_2, column_3 
    FROM TABLE_A 
WHERE id = 1 

而且,如果需要的話 - 你可以使用靜態定義的值,以及:

INSERT INTO TABLE_B 
    (column_1, column_2, column_3, column_4) 
SELECT column_1, column_2, 0, column_3 
    FROM TABLE_A 
WHERE id = 1 
+0

我已經編輯我的問題,你能回答的編輯請再次提供版本? – Husky110 2010-10-11 18:25:11

+1

@ Husky110:查看更新。 – 2010-10-11 18:27:38