[英]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.