如果你习惯mssql的写法,那么在写mysql的时候,可能会发现一个有很意思的问题。比如现在我写了这样一个sql:
update dhc_odds set cfirst_matchdatetime=(select cmatchdatetime from(select min(cmatchdatetime) from dhc_odds where cbatch_no=’wed002′) as b )
where cbatch_no=’wed002′
当运行后,mysql就会提示说“you can’t specify target table ‘dhc_odds’ for update in from clause”,具体的意思就是说不能在同一语句中先select出同一表中的某些值,再update这个表。根据网上高手的经验,可以把上面这个语句写成下面这样就行:
update dhc_odds a ,(select min(cmatchdatetime) as ma from dhc_odds where cbatch_no=’wed002′) b
set a.cfirst_matchdatetime=b.ma
where a.cbatch_no=’wed002′
另外一个mysql的例子:
update wms_cabinet_form set cabf_enabled=0
where cabf_id in (
select wms_cabinet_form.cabf_id from wms_cabinet_form
inner join wms_cabinet on wms_cabinet_form.cabf_cab_id = wms_cabinet.cab_id
inner join wms_cabinet_row on wms_cabinet.cab_row_id =wms_cabinet_row.row_id
where wms_cabinet_row.row_site_id=27 and wms_cabinet_form.cabf_enabled=1)
上面这个sql运行的时候同样会出现you can’t specify target table这样类似的错误提示,我们也可以把他改成这样:
update wms_cabinet_form set cabf_enabled=0 where cabf_id in (
select a.cabf_id from (select tmp.* from wms_cabinet_form tmp) a
inner join wms_cabinet b on a.cabf_cab_id = b.cab_id
inner join wms_cabinet_row c on b.cab_row_id = c.row_id
where c.row_site_id=29 and a.cabf_enabled=1)
重点在 select a.cabf_id from (select tmp.* from wms_cabinet_form tmp) a ,select tmp.* from wms_cabinet_form tmp 作为子集,然后再select a.cabf_id from 子集,这样就不会select和update都是同一个表。
腾讯云服务器部署网站麻烦查一下这个域名的备案用户名以及绑定的邮箱和手机景安虚拟主机怎么样excel2010下拉菜单怎么做?教你在Excel2010创建下拉菜单的方法好的域名有什么特点?新手注册好域名要了解什么?阿里云服务器怎么更改邮箱网站建设过程中容易出现的几个错误吐嘈一下阿里云服务器的硬盘升级