再来说说,这个问题吧。我在本地实验如下: Table:a、b Structure:
+-----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| timestamp | date | YES | | NULL | |
+-----------+---------+------+-----+---------+-------+ Operations:
mysql>mysqldump -w "(id) in (select id from b where b.timestamp < '2009-01-01')" test a --lock-all-table -uroot -p >/tmp/a_b.sql
mysql>system less /tmp/a_b.sql
----omitted-------
-- Table structure for table `a`
--
DROP TABLE IF EXISTS `a`;
CREATE TABLE `a` (
`id` int(11) DEFAULT NULL,
`timestamp` date DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
-- Dumping data for table `a`
--
-- WHERE: (id) in (select id from b where b.timestamp < '2009-01-01')
LOCK TABLES `a` WRITE;
INSERT INTO `a` VALUES (1,'2007-01-23'),(3,'2009-12-30');
UNLOCK TABLES;
-----omitted------ 分析:通过这些操作,我们可以很清楚的了解mysqldump是如何通过-w来构造执行语句的。-w后面双引号中的部分就是你要where的条件,而test a,告诉mysqldump我们要操作的是数据库test中的表a。其实,mysqldump执行了这么一条sql语句:
'SELECT * FROM `a` WHERE (id) in (select id from b where b.timestamp < '2009-01-01');'
此外,--lock-all-table这个选项也很必要。mysqldump -uroot -p123456 --default-character-set=utf8 --opt --single-transaction --extended-insert=false -R test a "-wnews.Adddate > '2010-05-12 10:17:00'" | gzip > /bak/autodatabak/test_a_201005261200.gz
mysqldump -uroot -p123456 --default-character-set=utf8 --opt --single-transaction --extended-insert=false -R test b -w "(pid) in (select id from a where news.Adddate > '2010-05-12 10:17:00')" | gzip > /bak/autodatabak/test_b_201005261200.gz