簡體   English   中英

MYSQL:選擇其中的/和/或帶有兩個表

[英]MYSQL: select where / and / or with two tables

我正在嘗試使以下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)
);

在表中唯一的tc_appointment條目中, tcAppointment.tc_message_id為null。 我正在嘗試讓它返回,因為counsellor_id = 502並且第二個OR語句為true

由於tc_message as tcMessage子句和第一個AND / OR語句,我似乎被卡住了,但是我不確定為什么。 誰能指出我正確的方向?

嘗試聯接2個表並使用“位置”:

 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM