简体   繁体   English

SQL查询删除多个表并从多个表中选择

[英]SQL query to drop multiple tables and select from multiple tables

I have two SQL databases in phpMyadmin, db1 and db2. 我在phpMyadmin中有两个SQL数据库,db1和db2。 Say db1 has 3 tables: 1_a, 1_b, 1_c; 假设db1有3个表:1_a,1_b,1_c; and db2 has 3 tables: 2_d, 2_e, 2_f. db2具有3个表:2_d,2_e,2_f。 I would like to transfer my db1's 1_a,1_b,1_c over to db2 and replace with db2's 2_d,2_e,2_f. 我想将db1的1_a,1_b,1_c转移到db2并替换为db2的2_d,2_e,2_f。

So, firstly, I will have to select 1_a, 1_b, 1_c tables from db1, and export them as a SQL file. 因此,首先,我将不得不从db1中选择1_a,1_b,1_c表,并将其导出为SQL文件。 Afterwards, I will have to select 2_d,2_e,2_f tables from db2, and drop them from db2. 之后,我将不得不从db2中选择2_d,2_e,2_f个表,并从db2中删除它们。 Thirdly, I just have import the file from first step to db2. 第三,我只是将文件从第一步导入到db2。

I am wondering what are the correct SQL statements for first and second steps? 我想知道第一步和第二步的正确SQL语句是什么? Assuming all tables that I am going to move around have their prefix like 1_x and 2_x, and there are some other tables in db1 and db2 that has no prefix and I do not want to touch them. 假设我要移动的所有表都有其前缀,例如1_x和2_x,并且db1和db2中还有一些其他表没有前缀,因此我不想触摸它们。 Appreciate your answers. 感谢您的回答。

you can use mysqldump to backup 3 tables or use select ... into outfile syntax for csv output: 您可以使用mysqldump备份3个表,或使用select ... into outfile语法中以获取csv输出:

SELECT * FROM `1_a` INTO '/home/1_a.txt'
SELECT * FROM `1_b` INTO '/home/1_b.txt'
SELECT * FROM `1_c` INTO '/home/1_c.txt'

or better yet use phpmyadmin backup function . 或更好的使用phpmyadmin备份功能 and afterwards you should use 然后您应该使用

DROP TABLE `2_d`;
DROP TABLE `2_e`;
DROP TABLE `2_f`;

to drop tables in second database 在第二个数据库中删除表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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