ERROR 1093 (HY000) 错误的原因及解决方法!

今天在使用存储过程删除重复记录的时候,报如上错误

beginTime:  20131216_14:28:08
Begin delete the pim_sms_info_1's duplicate data....

ERROR 1093 (HY000) at line 8: You can't specify target table 'pim_sms_info_1' for update in FROM clause

在网上找了一下原因,原来对本表数据的重复数据删除时,需要把子查询当成个临时表启个别名再套一层,才能正确使用,于是果断修改,修改后语法如下:

修改前:

DELETE FROM pim_sms_info_1 WHERE userid=v_userid AND `DATE`=v_date and smsid not IN (SELECT max(smsid)FROM pim_sms_info_1 info WHERE info.userid=v_userid AND info.DATE=v_date AND info.address=v_address AND info.smsid < v_max_smsid);

修改后:

DELETE FROM pim_sms_info_1 WHERE userid=v_userid AND `DATE`=v_date AND smsid NOT IN( SELECT MID FROM (SELECT MAX(smsid) MID  FROM pim_sms_info_1 WHERE userid=v_userid AND `DATE`=v_date) c);


你可能感兴趣的:(ERROR 1093 (HY000) 错误的原因及解决方法!)