考虑两个表
表a:
Fields :
- a_ID1
- a_Status
- a_Key
表b:
Fields :
- b_ID
- b_Status
- FK_a_key
有没有办法从两个表中删除一组关于以下条件的行:
删除FK_a_key = xxx and a_key = xxx
的每一行
但是,只有当这些行的状态字段都不是“2”时才会出现?
答案 0 :(得分:0)
几个not exists
来电应该可以解决问题:
DELETE FROM a
WHERE a_key = 'xxx' AND
NOT EXISTS (SELECT *
FROM a
WHERE a_key = 'xxx' AND a_status != 2) AND
NOT EXISTS (SELECT *
FROM b
WHERE FK_a_key = 'xxx' AND b_status != 2)
DELETE FROM b
WHERE b_key = 'xxx' AND
-- Frankly, both these conditions are are redundant -
-- they've been checked in the previous statement
NOT EXISTS (SELECT *
FROM a
WHERE a_key = 'xxx' AND a_status != 2) AND
NOT EXISTS (SELECT *
FROM b
WHERE FK_a_key = 'xxx' AND b_status != 2)