我想让以下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
语句,我似乎陷入困境,但我不确定原因。有人能指出我正确的方向吗?
答案 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