-- 假设有两个表 table1 和 table2
-- 创建临时表 temp_table,用来存储 table1 的数据
CREATE TEMPORARY TABLE temp_table AS SELECT * FROM table1;
-- 清空 table1,为 table2 的数据腾出空间
DELETE FROM table1;
-- 将 table2 的数据插入到 table1 中
INSERT INTO table1 SELECT * FROM table2;
-- 清空 table2
DELETE FROM table2;
-- 将 temp_table 的数据插入到 table2 中,此时 table2 为原先 table1 的数据
INSERT INTO table2 SELECT * FROM temp_table t;
-- 删除临时表
DROP TEMPORARY TABLE IF EXISTS temp_table;
mysql 2个表互换数据
最新推荐文章于 2025-03-11 11:28:52 发布