這將update
「YYY」而不是「ZZZ」通過使用not exists()
作爲where
子句的一部分,以確保具有相同name
一排已經爲xyId = 300
記錄不存在。
update t
set xyId = 300
where xyId = 200
and not exists (
select 1
from t as i
where i.name = t.name
and i.xyId = 300
);
如果你想delete
行與xyId = 200
有與xyId = 300
相應的記錄,你可以使用exists()
像這樣:
delete
from t
where xyId = 200
and exists (
select 1
from t as i
where i.name = t.name
and i.xyId = 300
);
rextester演示:http://rextester.com/IIQL1351
結果從更新:
+----+------+------+
| id | name | xyId |
+----+------+------+
| 1 | xxx | 100 |
| 2 | yyy | 300 |
| 3 | zzz | 300 |
| 4 | zzz | 200 |
+----+------+------+
個
結果從刪除:
+----+------+------+
| id | name | xyId |
+----+------+------+
| 1 | xxx | 100 |
| 2 | yyy | 300 |
| 3 | zzz | 300 |
+----+------+------+
嗯即時得到零行更新..好像你的邏輯是正確的,但。 – user1336827
@ user1336827我的查詢中有一個錯字,很抱歉 – SqlZim