问题
I am using a mysql 5.0.77 database using the query:
Delete from IPADDRESS
where visitdate Not in (SELECT max(visitdate) FROM IPADDRESS WHERE USERNAME='MGSH0002')
and USERNAME='MGSH0002'
I am getting this error when executed:
You can't specify target table IPADDRESS for update in from clause
回答1:
Certainly not the best solution but for your problem this will do the trick:
delete i1 from
IPADDRESS i1,
IPADDRESS i2
where
i1.username = i2.username
and i1.username = 'MGSH0002'
and i1.visitdate < i2.visitdate
An alternative and much more smarter solution is the following statement:
delete i1 from
IPADDRESS i1,
(select max(visitdate) visitdate from IPADDRESS where username = 'MGSH0002') temp
where
i1.username = 'MGSH0002'
and i1.visitdate < temp.visitdate
回答2:
You can't, unfortunately MySql doesn't allow this.
Currently, you cannot update a table and select from the same table in a subquery.
From:
http://dev.mysql.com/doc/refman/5.0/en/update.html
来源:https://stackoverflow.com/questions/9907484/how-to-use-subquery-in-delete-update-query-in-mysql