mysql update使用子查詢

2021-08-05 22:49:56 字數 707 閱讀 1216

今天我像以前操作oracle寫了乙個update sql:

update device_user a set a.scene_id=null

where a.id not

in(select

min(t.id) from device_user t group

by t.device_id);

根據子查詢的結果,更新表中的乙個字段。

在mysql

資料庫中執行後報錯:

error code: 1093. you can't specify target table 'a' for update in from clause
一直沒弄明白這個錯誤,然後經過多次度娘後,發現mysql update時,更新的表不能在set和where中用於子查詢。

修改上面的sql為mysql資料庫支援的方式:

update device_user du, 

(select id from device_user where id not

in(select

min(t.id) from device_user t group

by t.device_id)) b

set du.scene_id=null

where du.id=b.id;

mysql update使用子查詢

今天我像以前操作oracle寫了乙個update sql update device user a set a.scene id null where a.id not in select min t.id from device user t group by t.device id 根據子查詢的...

mysql update使用子查詢

今天我像以前操作oracle寫了乙個update sql update device user a set a.scene id null where a.id not in select min t.id from device user t group by t.device id 根據子查詢的...

使用Filtered Indexes提高查詢效能

通常我們會遇到這樣的情況,一張表中包含上百萬條的資料,但是每次我們只查詢一小部分的資料。比如一列只有少部分null值,每次我們都需要將null值找出來進行處理。或者我們有狀態標誌位,需要取flag對資料處理。由於資料大部分是重複的,所以對於整個列做索引代價是非常大的,而且對查詢效能提公升可能不大。慶...