mysql如何多表联合按条件导出数据?

例如,我想导出table1中abc_id=1的记录以及与它联结的table2表中的相应数据,我用mysqldump的where参数似乎不可以联结表:
mysqldump -u root abc table1 table2 --where="abc_id=1 and table1_id in (select id from table1 where abc_id=1)" > example.sql
mysqldump: Couldn't execute 'SELECT /*!40001 SQL_NO_CACHE */ * FROM table1 WHERE abc_id=1 and table1_id in (select id from table1 where abc_id=1)': Unknown column 'table1_id' in 'IN/ALL/ANY subquery'

应该怎样导出这两个表中的数据呢?

-w 选项确实不支持join

这是个定位问题,mysqldump是致力于,快速备份还原数据库的。如果你注意一下mysqldump导出的mysql文件,你会发现,里面有create insert,实际不是导出数据,而是,一系列语句直接运行就能还原数据,或者还原表结构。

这就解释了为什么设计mysqldump的时候为什么没有-w 支持join的选项,因为,备份完了,运行还原的时候还原什么呢?

也就是说,如果,楼主只是想纯导出数据,而不是想用来备份还原的话,直接select就行了,而要想备份还原的话,直接两个表备份就行

也是个人理解哈

你new一个table,然后要mysqldump指定到单表就可以了吧

(1) create table table3 as (select * from table1 where abc_id=1);

(2) create table table4 as (select * from table2 where table1_id in (select id from table1 where abc_id=1));

mysqldump --single-transaction dbname table3 table4 > dbname.table3_table4.2011.05.26