MYSQL:选择/和/或两个表的位置

时间:2015-09-02 16:47:17

标签: mysql select

我想让以下SQl工作:

SELECT * FROM tc_appointment as tcAppointment, tc_message as tcMessage
WHERE tcAppointment.counsellor_id = 502
AND 
(
    (tcAppointment.tc_message_id = tcMessage.id AND tcMessage.marked_read = false AND tcMessage.sender_id != 502)
    OR
    (tcAppointment.cancelled = true AND tcAppointment.cancelled_acknowledged = false)
    OR 
    (tcAppointment.confirmed = false)
);

tcAppointment.tc_message_id在表中唯一的tc_appointment条目中为空。我试图让它返回为counsellor_id = 502,第二个OR声明为true

由于tc_message as tcMessage子句和第一个AND / OR语句,我似乎陷入困境,但我不确定原因。有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:1)

尝试加入2个表并使用' Where':

 SELECT * FROM tc_appointment as tcAppointment 
    LEFT JOIN tc_message as tcMessage 
    ON 
    ((tcAppointment.tc_message_id = tcMessage.id AND tcMessage.marked_read = false AND tcMessage.sender_id != 502)
    OR
        (tcAppointment.cancelled = true AND tcAppointment.cancelled_acknowledged = false)
        OR 
        (tcAppointment.confirmed = false)
) 

    WHERE tcAppointment.counsellor_id = 502