我有两张桌子:
SELECT
d.id,
d.permission_lookup_id,
d.permission_object_id,
f.id,
f.permission_lookup_id,
f.permission_object_id
FROM
folders f
LEFT JOIN
documents d
ON f.id = d.folder_id
WHERE
d.permission_lookup_id != f.permission_lookup_id OR
d.permission_object_id != f.permission_object_id
从理论上讲,如果一切正常,这应该不会返回任何结果,但事实并非如此,我收到了数十万个结果(目前为 407,343)。我需要更新documents
表,以便permission_lookup_id
匹配folders
'spermission_lookup_id
和同样 for permission_object_id
,基于documents.parent_id
外键约束。
我尝试了以下查询但收效甚微:
UPDATE
folders f
LEFT JOIN
documents d
ON f.id = d.folder_id
SET
d.permission_lookup_id = f.permission_lookup_id,
d.permission_object_id = f.permission_object_id
WHERE
d.permission_lookup_id != f.permission_lookup_id OR
d.permission_object_id != f.permission_object_id
运行后它告诉我有 70,986 行受到影响,407343 行匹配,但我仍然有第一个选择查询的结果。
编辑:以下两个查询都返回 0 行:
SELECT * FROM folders WHERE permission_lookup_id IS NULL OR permission_object_id IS NULL;
SELECT * FROM documents WHERE permission_lookup_id IS NULL OR permission_object_id IS NULL;
空白值也是如此。
编辑 2:在我原来的选择/更新查询中犯了一个业余错误。在 WHERE 子句中,我有:
WHERE
d.permission_lookup_id != f.permission_lookup_id OR
d.permission_object_id != f.permission_lookup_id
我应该有的地方
WHERE
d.permission_lookup_id != f.permission_lookup_id OR
d.permission_object_id != f.permission_object_id
这导致我收到误报,并且只会导致更新通过并更新每一列,无论是否需要。对困惑感到抱歉。