简体   繁体   English

MySQL更新一个表,如果它们不相等,则使用另一个表中的值

[英]MySQL UPDATE one table with values from another table if they're not equal

I have two tables: 我有两张桌子:

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

In theory this should return nothing if all is well, but it isn't, I receive hundres of thousands of results (currently 407,343). 从理论上讲,如果一切顺利的话,这应该不会返回,但事实并非如此,我收到了成千上万的结果(目前为407,343)。 I need to update the documents table so that permission_lookup_id matches folders 's permission_lookup_id and likewise for permission_object_id , based off of documents.parent_id as the foreign key constraint. 我需要更新documents表,以便permission_lookup_id匹配folderspermission_lookup_id ,同样也匹配permission_object_id ,基于documents.parent_id作为外键约束。

I tried the following query with little success: 我尝试了以下查询但收效甚微:

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

After running that it tells me that 70,986 rows affected, and 407343 rows matched, yet I still have results from the first select query. 运行后,它告诉我70,986行受影响,407343行匹配,但我仍然有第一个选择查询的结果。

EDIT: Both of the following queries return 0 rows: 编辑:以下两个查询返回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;

Same goes for blank values as well. 空白值也是如此。

EDIT 2: There was an amateur mistake made in my original select/update queries. 编辑2:我原来的选择/更新查询中出现了一个业余错误。 In the WHERE clause, I had: 在WHERE子句中,我有:

WHERE
    d.permission_lookup_id != f.permission_lookup_id OR
    d.permission_object_id != f.permission_lookup_id

Where I should have had 我应该在哪里

WHERE
    d.permission_lookup_id != f.permission_lookup_id OR
    d.permission_object_id != f.permission_object_id

That was causing me to receive false positives, and merely would have caused nothing more than to have the updates go through and update every column regardless of whether or not it needed to. 这导致我收到误报,只会导致更新通过并更新每一列,无论是否需要。 Sorry for the confusion. 对困惑感到抱歉。

How about this alternative syntax: 这个替代语法怎么样:

UPDATE 
    folders f, documents d
SET
    d.permission_lookup_id = f.permission_lookup_id,
    d.permission_object_id = f.permission_object_id
WHERE
    f.id = d.folder_id AND (
    d.permission_lookup_id != f.permission_lookup_id OR
    d.permission_object_id != f.permission_lookup_id)

The manual says JOIN clauses are ok, but I'm curious to see if there's any difference. 手册说JOIN条款没问题,但我很想知道是否有任何区别。

The preceding example shows an inner join that uses the comma operator, but multiple-table UPDATE statements can use any type of join permitted in SELECT statements, such as LEFT JOIN. 前面的示例显示了使用逗号运算符的内部联接,但多表UPDATE语句可以使用SELECT语句中允许的任何类型的联接,例如LEFT JOIN。

Is is possible that the permission_lookup_id from either table has NULL values? 来自任一表的permission_lookup_id是否可能具有NULL值? If so, you might want to try using the NULL-safe operator: 如果是这样,您可能想尝试使用NULL-safe运算符:

WHERE
    (NOT d.permission_lookup_id <=> f.permission_lookup_id) OR
    (NOT d.permission_object_id <=> f.permission_lookup_id)

UPDATE UPDATE

Try one field at time just to see and use the following syntax (this might take a longer to run): 一次尝试一个字段只是为了查看并使用以下语法(这可能需要更长的时间才能运行):

UPDATE folders f
SET f.permission_lookup_id =COALESCE((SELECT d.permission_lookup_id FROM documents d
                                      WHERE f.id = d.folder_id
                                      AND d.permission_lookup_id != f.permission_lookup_id),
                                      f.permission_lookup_id)

Try using inner join in your query instead of left join and please let me know that whether your id field has null values? 尝试在查询中使用内部联接而不是左联接,请告诉我您的id字段是否具有空值? If yes, then it needs a different approach 如果是,那么它需要一种不同的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM