MySQL修改并选取同一个表的方法
来源:程序员人生 发布时间:2014-06-01 05:04:07 阅读次数:3428次
在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
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠