它可以是更短:
UPDATE tableB b
SET b.`status` = 'done'
WHERE 'done' = ALL(SELECT `status` FROM tableA a WHERE a.key = b.id)
測試數據在這裏:
drop table if exists tableA;
create table tableA(id int, `key` varchar(10), status varchar(10));
insert into tableA values (1, 'dg12', 'done'), (2,'dg12', '');
drop table if exists tableB;
create table tableB(id varchar(10), name varchar(10), status varchar(10));
insert into tableB values ('dg12','Dummy', '');
查詢從上面執行:
0 rows affected
update tableA set status='done' where id = 2;
從上面的前查詢ecuted:
1 row affected
瞭解更多關於子查詢與ALL
here。
如果tableA key =「dg12」和status =「done」中的關係狀態 - >刪除tableB中的行,其中id =「dg12」? – ClydeFrog
所以約束條件是表A在表B更新之前必須有兩個記錄'status'!= null? – ethrbunny
@ethrbunny是這樣的... –