select DISTINCT first_name, last_name, picture, last_active, id_participante1, id_participante2, id_user, [message], dataHora from chat_b_users inner join utilizadores
on chat_b_users.id_participante2 = utilizadores.id_user
left join chat_talks on chat_b_users.id_chat = chat_talks.id_chat
where id_participante1 = 1
union all
select DISTINCT first_name, last_name, picture, last_active, id_participante1, id_participante2, id_user, [message], dataHora from chat_b_users inner join utilizadores
on chat_b_users.id_participante1 = utilizadores.id_user
left join chat_talks on chat_b_users.id_chat = chat_talks.id_chat
where id_participante2 = 1 order by last_active DESC
如何選擇不同的值?SQL Query返回其中之一
我需要返回所有這些數據,即使是空值,但對每個用戶,我怎麼能做到這一點?
結果:
,你可以在圖片中看到,我已經從同一用戶進行的兩次聊天記錄,我只希望每個之一。
您是否試過'SELECT DISTINCT'? – 2014-09-10 09:43:33
是的,我試過了,相同的結果 – Severiano 2014-09-10 09:45:12
我明白了,'DISTINCT'不起作用,因爲組合不同。你的時間戳使他們不會失去意義。用'last_active'試試'MAX',看看它是否有幫助。看看[這個答案](http://stackoverflow.com/questions/5391564/how-to-use-distinct-and-order-by-in-same-select-statement) – 2014-09-10 09:47:51