国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

mysql 一個較特殊的問題:You can't specify target table 'wms_cabinet_form'

2020-01-19 00:12:58
字體:
來源:轉載
供稿:網友
今天在寫 mysql 遇到一個比較特殊的問題。
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)

運行時提出如下提示: You can't specify target table 'wms_cabinet_form' for update in FROM clause

運行 in 里面的 select 字句:

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

可以正確 select 正確結果。再把結果直接寫到 in 里面,改后語句如下:
update wms_cabinet_form set cabf_enabled=0 where cabf_id in ('113','114','115'),再運行可以正確執行更新。
到這一步開始想不明白,為什么用 select 子句運行會出錯呢?以前在 mssql 這種寫法是很常見的。
沒辦法了,唯有動用 baidu。找到兩條記錄。

原來原因是:mysql中不能這么用。 (等待mysql升級吧)。那串英文錯誤提示就是說,不能先select出同一表中的某些值,
再update這個表(在同一語句中)。 也找到替代方案,重寫改寫了 sql 。

改寫后的 sql 如下所示,大家仔細區別一下。

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 都是同一個表。致此問題得到完美解決。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 西贡区| 乐陵市| 开远市| 敦化市| 天峻县| 鸡东县| 香格里拉县| 景泰县| 甘孜县| 江门市| 岳池县| 建水县| 长寿区| 南陵县| 高淳县| 朝阳县| 万全县| 剑川县| 上栗县| 青铜峡市| 江安县| 津南区| 松潘县| 宁武县| 民权县| 綦江县| 施甸县| 阿瓦提县| 图们市| 瑞昌市| 酉阳| 鹿泉市| 肃南| 林周县| 大石桥市| 重庆市| 靖边县| 济宁市| 德州市| 乐山市| 台前县|