无法加入2个表

时间:2013-07-20 14:49:29

标签: sql join

我已经尝试过我可能做到的事情,但我很难弄清楚这个查询的问题。

当我执行查询时,它会从表deleted_messages中重复显示行。我正在使用private_messages加入deleted_messages表。 deleted_messages保留已被用户完全删除的邮件。

SELECT private_messages.EA_MID
    ,private_messages.EA_to
    ,private_messages.EA_from
    ,private_messages.EA_DateSent
    ,private_messages.EA_Subject
    ,private_messages.EA_Message
    ,private_messages.EA_Opened
    ,private_messages.EA_RecipientDeleted
    ,private_messages.EA_SenderDeleted
    ,deleted_messages.EA_DMID
    ,deleted_messages.EA_UID
    ,deleted_messages.EA_MID
    ,deleted_messages.EA_Date
FROM deleted_messages
LEFT JOIN private_messages ON (deleted_messages.EA_UID = private_messages.EA_to)
    OR (deleted_messages.EA_UID = private_messages.EA_from)
WHERE (
        (
            private_messages.EA_to = '$uid'
            AND private_messages.EA_RecipientDeleted = '1'
            )
        OR (
            private_messages.EA_from = '$uid'
            AND private_messages.EA_SenderDeleted = '1'
            )
        )

1 个答案:

答案 0 :(得分:0)

SELECT /* field omitted  */
FROM deleted_messages
LEFT JOIN private_messages 
    ON (deleted_messages.EA_UID = private_messages.EA_to 
         AND private_messages.EA_RecipientDeleted = '1')
    OR (deleted_messages.EA_UID = private_messages.EA_from 
         AND private_messages.EA_SenderDeleted = '1')
WHERE /* omitted */