如果其他表没有相关数据,则更新多个表

时间:2013-07-28 14:01:58

标签: mysql

我有4张桌子。

我想更新所有表格,因为我正在使用连接。

仅当所有表都有值时,查询才有效。如果任何一个表没有记录,那么它没有更新其他表

我的查询

update post_message_users
LEFT JOIN post_messages 
ON post_message_users.messageid = post_messages.messageid 
LEFT JOIN comments
ON post_message_users.messageid = comments.comment_on 
LEFT JOIN likes
ON post_message_users.messageid =likes.element_id 
SET post_message_users.status='deleted',
post_message_users.deleted_time=NOW(),
post_messages.status = 'deleted' ,
post_messages.delete_time = now(),
comments.status ='deleted', 
likes.status='deleted',
likes.delete_time=now()
WHERE 
 comments.element_type ='Message' 
AND
likes.element_type ='Message' 
and post_message_users.messageid = 33 
and post_message_users.received_by= 3

请查看我应该做出哪些更改,以便我正常工作。

1 个答案:

答案 0 :(得分:1)

您的where子句正在撤消left outer join,将其转换为内部联接。要解决此问题,请将条件移至on子句:

update post_message_users
       LEFT JOIN post_messages 
                 ON post_message_users.messageid = post_messages.messageid 
       LEFT JOIN comments
                 ON post_message_users.messageid = comments.comment_on and
                    comments.element_type = 'Message'
       LEFT JOIN likes
                 ON post_message_users.messageid = likes.element_id and
                    likes.element_type = 'Message'
SET post_message_users.status='deleted',
    post_message_users.deleted_time=NOW(),
    post_messages.status = 'deleted' ,
    post_messages.delete_time = now(),
    comments.status ='deleted', 
    likes.status='deleted',
    likes.delete_time=now()
WHERE post_message_users.messageid = 33 and post_message_users.received_by= 3;