enter image description here

在这里一起工作" /> enter image description here

在这里一起工作 - 源码之家" />

GROUPBY和ORDER BY不是这样

enter image description here

在这里一起工作

问题描述:

我有记录的是我的查询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

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

一个独特的身份证为什么倒投票? –

您尚未使用任何聚合函数,因此您的group by仅返回第1个数据集。有几种方法以解决它

  1. 删除组,只是如果你想通过我们conversation_id
  2. 使用聚合函数来排序
+0

我只需要一个结果这就是为什么 – hellosheikh