它可以更短:
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;
从上面执行的查询:
1 row affected
ALL 在此处阅读有关子查询的更多信息。