我想獲取所有用戶名和last_messages與當前用戶的對話。MySQL左連接獲取最後的消息和用戶名
我有兩個表:
主題
用戶
因此,如果當前用戶的ID是1,我應該得到的行對於鮑勃和麥克,但我得到空的結果。你能幫我找到這個錯誤嗎?
SELECT * FROM thread
LEFT JOIN user
ON ((thread.user1_id!=current_user AND thread.user1_id=user.id)
OR (thread.user2_id!=current_user AND thread.user2_id=user.id))
WHERE current_user = 1;
SQL服務器行試試? MySQL的?甲骨文?什麼DBMS? –
當'current_user'不是'1'時,您的查詢將不返回任何行。 –
http://sqlfiddle.com/#!9/0ab85c/1 - 顯然(?)只加入末尾的名字表 – Strawberry