小编use*_*054的帖子

通过desc选择不同的值顺序

我要选择不同customer_id的任意顺序s.notime或者无论如何

s.no     customer_id         time
1        3                   100001
2        2                   100002
3        4                   100003
4        3                   100004
5        2                   100005
Run Code Online (Sandbox Code Playgroud)

我在用

select distinct(customer_id) from table_name order by time DESC
Run Code Online (Sandbox Code Playgroud)

它给出了答案,4 2 3但我想它应该是2 3 4

mysql sql

4
推荐指数
1
解决办法
9959
查看次数

以desc顺序选择唯一行

我想按时间DESC选择每个id顺序插入的最后一行

s.no     id    message           status     time
1        3     this is msg 1     unread     100001
2        2     this is msg 2     read       100002
3        4     this is msg 3     read       100003
4        3     this is msg 4     unread     100004
5        2     this is msg 5     read       100005
6        3     this is msg 6     unread     100006
Run Code Online (Sandbox Code Playgroud)

我在用

select * from table group by id order by MAX(time) DESC
Run Code Online (Sandbox Code Playgroud)

它给出了正确的id顺序,但行互换了

我希望它像:

s.no     id    message           status     time
6        3     this is msg …
Run Code Online (Sandbox Code Playgroud)

mysql sql greatest-n-per-group

0
推荐指数
1
解决办法
123
查看次数

标签 统计

mysql ×2

sql ×2

greatest-n-per-group ×1