mysql - 按 desc 选择不同的值顺序

标签 mysql sql

我想通过 s.notime 或任何方式选择不同的 customer_id 订单

s.no     customer_id         time
1        3                   100001
2        2                   100002
3        4                   100003
4        3                   100004
5        2                   100005

我正在使用

select distinct(customer_id) from table_name order by time DESC

它给出的答案是 4 2 3 但我希望它应该是 2 3 4

最佳答案

所以你的问题陈述是“你想要 customer_id 的列表,从它们的最大时间值降序排列”,对吧?

SELECT customer_id, MAX(time)
FROM table_name
GROUP BY customer_id
ORDER BY MAX(time) DESC

关于mysql - 按 desc 选择不同的值顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19334128/

相关文章:

python - 更新数据库时where子句中的未知列

php - 从 Joomla 中的回调 URL 更新 MySQL 表

mysql - SQL 对表中所有列的不同值进行计数并计算空值

sql - 查找所有具有 ROWGUIDCOL 的表

mysql - 我该如何编写这个 MySQL 查询?

mysql - REGEXP 匹配包含 + 和空格的单词列表

mysql - 如何添加 'created_at' 和 'updated_at' 列?

SQL Case 语句正确的别名

mysql - 查询自引用表中的父子

mysql - 没有结果显示 AND 错误以及 where 子句