如何在几个表中为选择性值集采取数据库转储

时间:2013-03-01 10:36:51

标签: mysql mysqldump

create table foo (id, val1, user_id,...)
create table bar (id, foo_id, val2, ...)
create table baz (id, bar_id, val3, ...)

select * from foo where user_id = 1;
select * from bar where id in (select id from foo where user_id = 1)
select * from baz where id in (for all the above bars)

如何编写执行上述

的转储命令

1 个答案:

答案 0 :(得分:1)

mysqldump -h yourhost -u username -p yourDatabase foo --where="user_id = 1" >dumpFile1
mysqldump -h yourhost -u username -p yourDatabase bar --where=" id in (select id from foo where user_id = 1)" >dumpFile2
mysqldump -h yourhost -u username -p yourDatabase baz --where="id in (select * from bar where id in (select id from foo where user_id = 1))" >dumpFile3

另一种选择是导出为CSV文件。

select * from foo where user_id = 1 INTO OUTFILE 'C:/whatever'

或类似的东西。请查看manual

<强>更新

未经测试,但您可以尝试一下吗?

mysqldump -h yourhost -u username -p yourDatabase foo bar baz
--where="foo.user_id = 1" 
--where="bar.id in (select id from foo where user_id = 1)" 
--where="baz.id in (select * from bar where id in (select id from foo where user_id = 1))" 
>dumpFile

更新2:

在mysqldump的manual中,我看不到多个WHERE子句。所以我担心你必须分多步完成。