我想使用group by获取mysql的最后一行

时间:2015-11-18 10:24:20

标签: mysql sql database mysqli

{
  "paths": {
    "/": {
      "post": {
        "x-auth-type": "Application & Application User",
        "x-throttling-tier": "Unlimited",
        "x-mycustom-extension": "someValue", <-- How to get access to this?
        "produces": [
          "application/json"
        ],
        "parameters": [

这是一个查询,它总是给我每条记录的第一个索引。但我想在每个组中得到最新的索引..我尝试了很多方法,但结果都没有。 用户: this is without grope by

this is with grope by

this is what i want after group by

this is what giving to me

3 个答案:

答案 0 :(得分:-1)

尝试这个代码

SELECT * FROM msg_messages m 
JOIN msg_status s on m.messageId = s.messageId 
JOIN msg_threads t ON t.threadId = m.threadId 
JOIN users u ON u.userId = s.userId 
WHERE t.threadId = 1 
GROUP BY u.userId ORDER BY `m`.`date` DESC LIMIT 0,1

答案 1 :(得分:-1)

怎么样:

SELECT * FROM msg_messages m 
JOIN msg_status s on m.messageId = s.messageId 
JOIN msg_threads t ON t.threadId = m.threadId 
JOIN users u ON u.userId = s.userId 
GROUP BY u.userId 
having max (`m`.`date`)
ORDER BY `m`.`date` DESC 

我从

修改了我的查询
having max (`t`.`threadId`)

having max (`m`.`date`)

答案 2 :(得分:-1)

您可以在DESC之后使用LIMIT 1,然后您将获得最后一条记录。 例: DESC 限制1;