簡體   English   中英

將一個表復制到另一個表並添加一個作為源表名的列

[英]copy a table to another and add a column that is the tablename of the source

我想將一些具有相同前綴的表復制到一個表中。 目標表的列應多於源表,即源表的名稱。

我試過這個:

INSERT INTO `tbl_dest` (`nr`,`n1`,`n2`,`n3`,`n4`,`n5`,`table`) 
SELECT `a`.`nr`, `a`.`n1`, `a`.`n2`, `a`.`n3`, `a`.`n4`, `a`.`n5`, `b`.`table`  
FROM `tbl_src` `a`, `INFORMATION_SCHEMA.TABLES`.`TABLE_NAME` `b`

但它運作不佳。
實際上,在tbl_dest中,對於源自tbl_src的所有數據,列表的值應該是“ tbl_src

任何想法?

您可以使用常量:

INSERT INTO `tbl_dest` (`nr`,`n1`,`n2`,`n3`,`n4`,`n5`,`table`) 
    SELECT `a`.`nr`, `a`.`n1`, `a`.`n2`, `a`.`n3`, `a`.`n4`, `a`.`n5`,
       'tbl_src' 
    FROM tbl_src a;

我還建議您不要使用所有反引號。 它們只是使查詢更難編寫和閱讀。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM