在MySQL裡,不可以修改同一個表並使用SELECT語句。可以選用JOIN來實現,例證如下
用SELECT的原語句:
update spms.user
set DirectReport=(select DirectReport from spms.user where UserName='%s')
where UserName in (select UserName from spms.user where DirectReport='%s')
選用JOIN來實現的語句:
update spms.user as u1
inner join spms.user as u2 on u1.DirectReport= u2.UserName
set u1.DirectReport=u2.DirectReport where u2.UserName='%s'
Reference: SQL Update Syntax http://dev.mysql.com/doc/mysql/en/UPDATE.html