2017-03-08 41 views
-3

我有記錄的是我的查詢GROUPBY和ORDER BY不是這樣</p> <p><a href="https://i.stack.imgur.com/qxrUL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qxrUL.png" alt="enter image description here"></a></p> <p>在這裏一起工作

SELECT `Chat`.`id`, 
     `Chat`.`sender_id`, 
     `Chat`.`receiver_id`, 
     `Chat`.`message`, 
     `Chat`.`datetime`, 
     `Chat`.`converstation_id` 
FROM  `gopher`.`chat` AS `Chat` 
WHERE ((`Chat`.`sender_id` = 10) 
OR  (`Chat`.`receiver_id` = 10)) 
GROUP BY converstation_id 
ORDER BY `Chat`.`id` DESC 

但這裏爲了現在不工作,這是我得到的結果運行以上查詢後

enter image description here

+0

使用順序你需要刪除'GROUP BY conversation_id' 。 – MrDarkLynx

+0

'ORDER BY DESC LIMIT 1'就是這樣。你爲什麼需要'GROUP BY'? –

+1

你期待/想要發生什麼? – Ilion

回答

-1
SELECT `Chat`.`id`, 
     `Chat`.`sender_id`, 
     `Chat`.`receiver_id`, 
     `Chat`.`message`, 
     `Chat`.`datetime`, 
     `Chat`.`converstation_id` 
FROM `gopher`.`chat` AS `Chat` 
WHERE ((`Chat`.`sender_id` = 10) OR (`Chat`.`receiver_id` = 10)) 
GROUP BY converstation_id 
ORDER BY `Chat`.`id` DESC LIMIT 0,1 
+1

一個獨特的身份證爲什麼倒投票? –

1

您尚未使用任何聚合函數,因此您的group by僅返回第1個數據集。有幾種方法以解決它

  1. 刪除組,只是如果你想通過我們conversation_id
  2. 使用聚合函數來排序
+0

我只需要一個結果這就是爲什麼 – hellosheikh

相關問題

 相關問題