比方說,我有類似結構的兩個表,但一個是空的,其他有信息的幾行:MySQL的 - 表中的數據複製到不相同的表
table1的
col1 | col2 | col3
red | cow | 1
blue | dog | 2
gray | pig | 3
表2
col1 | col2 | col3 | col4
表3
col1 | col2
嘗試:
insert into `table2` select * from `table1`
不會因爲不匹配的列數的工作,這同樣適用於表2代真表3。
更改SELECT語句的*部分不是用於動態目的的選項。所以一個解決方法必須是一個組合信息的修改後的SELECT。
是否有JOIN語句或一些會合並表的結構和數據,所以它看起來是這樣的:
select * from `table1`,`table2` (JOIN or some other statement)
col1 | col2 | col3 | col4
red | cow | 1 | NULL
blue | dog | 2 | NULL
gray | pig | 3 | NULL
select * from `table1`,`table3` (JOIN or some other statement)
col1 | col2
red | cow
blue | dog
gray | pig
基本上只是合併的列具有相同的名稱和抵消外配。同樣,它不能引用特定的列名來保持動態。這似乎是可行的,但我發現找不到答案是不可能的。
非常感謝任何人的幫助。
他也可以使用:'insert into table2 select table1。*,NULL from table1' –
但我絕對同意你對**總是使用**的評論。 –
DESCRIBE是我的最後一招,但這些答案使它看起來像是我唯一的選擇。感謝您的查詢,但我認爲我要求的語法比這些更復雜。 – user1032911