Delete from ... not exisits_ database

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.