如果你習慣mssql的寫法,那么在寫mysql的時候,可能會發(fā)現(xiàn)一個有很意思的問題。比如現(xiàn)在我寫了這樣一個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這個表。根據(jù)網(wǎng)上高手的經(jīng)驗,可以把上面這個語句寫成下面這樣就行:
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運行的時候同樣會出現(xiàn)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都是同一個表。
騰訊云服務器部署網(wǎng)站麻煩查一下這個域名的備案用戶名以及綁定的郵箱和手機景安虛擬主機怎么樣excel2010下拉菜單怎么做?教你在Excel2010創(chuàng)建下拉菜單的方法好的域名有什么特點?新手注冊好域名要了解什么?阿里云服務器怎么更改郵箱網(wǎng)站建設過程中容易出現(xiàn)的幾個錯誤吐嘈一下阿里云服務器的硬盤升級