Extended learning in sql

1.

When using mysql for the delete from operation, if the FROM clause of the subquery and the update/delete object use the same table, an error will occur. 


 DELETE FROM tab1 WHERE  id = ( SELECT MAX(id ) FROM tab1 ); 
ERROR 1093 (HY000): You can’t specify target table ‘tab1′ for update in FROM clause 

For the limitation of "same table", aside from efficiency, in most cases, it can be solved by adding an extra layer of select alias table


2. Delete duplicate data and keep one

delete from cst_customer where cst_name in (select cst_name from (SELECT  cst_name  FROM cst_customer GROUP BY cst_name having COUNT(cst_name)>1)aa)
and cst_id not in (select cst_id from (select min(cst_id) cst_id  from cst_customer GROUP BY cst_name having COUNT(cst_name)>1) qq);


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325852194&siteId=291194637
Recommended