Previous: Data revision requirements: Delete Table A, not appearing in table B, the primary key of table A is the foreign key of table B
Sql_1: Number of revisions to statistical data
Select COUNT (*) from
table_name_a a
where a.column1 <> ' haha ' and a.gmt_create>= ' 2013-12-02 00:00:00 ' C2/>and NOT EXISTS (SELECT * from Table_name_b b where a.id=b.a_id);
Sql_2: So take it for granted that the select in Sql_1 should be changed to delete
Delete from
table_name_a a
where a.column1 <> ' haha ' and a.gmt_create>= ' 2013-12-02 00:00:00 '
and NOT EXISTS (SELECT * from Table_name_b b where a.id=b.a_id);
Perform sql_2 return syntax error, Baidu found http://xuliangyong.iteye.com/blog/427998 solution-to the table from the individual name, change to Sql_3:
Delete from table_name_a where ID in
(SELECT ID from
table_name_a a
where A.column1 & lt;> ' haha ' and a.gmt_create>= ' 2013-12-02 00:00:00 ' and not
exists (SELECT * from Table_name_b b where a.id=b.a_ ID))
t);
Sql_2 the reason for the failure, in the MySQL official website http://dev.mysql.com/doc/refman/5.6/en/subqueries.html a pair of query instructions have such a sentence:
"In MySQL, you cannot modify a table and select from the same table in a subquery." This is applies to statements such as DELETE, INSERT, REPLACE, UPDATE
Delete,insert statement, if there is a subquery, the table in the subquery and the table in the Delete,insert operation cannot be the same.
But when I was in the native MySQL test environment experiment, it was successful.
Insert into table_a (id,name) VALUES (one, ' Lucy ');
Insert into table_a (id,name) VALUES (' Yanghong ');
Insert into table_a (id,name) VALUES (' xiaoming ');
Insert into table_a (id,name) VALUES (' Jack ');
Insert into Table_b (Id,a_id,score) values (1,11,85);
Insert into Table_b (Id,a_id,score) values (2,14,98);
Delete from table_a
where NOT EXISTS (SELECT * from Table_b where table_b.a_id=table_a.id);
Don't know why. It is the native MySQL version update.