简体   繁体   中英

MySQL Issue Copying from one table into another directly

I use the following code to copy from one table directly into another:

$transfer = $db->exec("INSERT INTO table2 SELECT * FROM table1 WHERE groupname = '$gname'");

The issue I have, however, is the ID field of both tables do not necessarily match (both auto-increment) and at times this may mean one temporary's tables ID# is higher than the final table.

I use php, pdo and mysql.

Is there any way around this?

明确说明您想要的列:

"INSERT INTO table2 (`col_1`,`col_2`) SELECT `col_1`, `col_2` FROM table1 WHERE groupname = '$gname'"
"INSERT INTO final_table SELECT * FROM temp_table WHERE temp_table.groupname = '$gname'"

Where's the issue here? If both are auto_increment, and like you said temp_table's id is HIGHER than perm_table, you'll end up getting the right effect.

You should also be aware that MySQL will block inserts to the select table:

http://dev.mysql.com/doc/refman/5.0/en/insert-select.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM