如果foreignKey和foreignKey2尚不存在,我有以下查询来更新设置new foreignKey的表记录。这应该很好,但是,如果它存在,我如何修改删除特定的pkID记录?
表格结构:
+----------------+
| table |
+----------------+
| pkID |
| foreignKey |
| foreignKey2 |
+----------------+
查询:
UPDATE table a
SET a.foreignKey = 2
WHERE a.pkID = 1234
AND NOT EXISTS (
SELECT 1
FROM table b
WHERE b.foreignKey = 2
AND b.foreignKey2 = a.foreignKey2
)