我正在将一个表迁移到一个新数据库中。存在fk约束问题,因为这两个表之间从来没有设置关系(tbl_contacts.contact_ID和tbl_communications.contact_ID,即fk)。我能看到所有的排

SELECT * FROM Farm.tbl_communication as S
LEFT JOIN Farm.tbl_contacts ON S.contact_ID = Farm.tbl_contacts.contact_ID
WHERE Farm.tbl_contacts.contact_ID IS NULL

我只是想删除那些行,我试着这样做:
    DELETE FROM Farm.tbl_communication
    WHERE Farm.tbl_communication.contact_ID (SELECT contact_ID
    FROM ( SELECT * FROM Farm.tbl_communication) as S
    LEFT JOIN Farm.tbl_contacts ON S.contact_ID = Farm.tbl_contacts.contact_ID
    WHERE Farm.tbl_contacts.contact_ID IS NULL)

但没用。我现在想得更好了,我将创建一个catch a ll联系人,以便更新所有通信。contact_ID使用新的catch all ID。问题是我不知道如何进行此操作。语法是什么?

最佳答案

DELETE FROM Farm.tbl_communication
WHERE NOT EXISTS (
    SELECT 1
    FROM  Farm.tbl_contacts
    WHERE Farm.tbl_contacts.contact_ID = Farm.tbl_communication.contact_ID);

…或者您可以通过将DELETE位更改为update来更新它们。

07-26 07:13