gpt4 book ai didi

php - Mysql 按两列排序

转载 作者:行者123 更新时间:2023-11-29 12:35:01 25 4
gpt4 key购买 nike

我有一个这样的表:

id     |date        |publisher
1 2014-11-07 100
2 2014-11-07 0
3 2014-11-07 100
4 2014-11-06 0
5 2014-11-06 100
6 2014-11-05 100
7 2014-11-05 0
8 2014-11-05 0
9 2014-11-05 100

我正在尝试得到这样的结果:

1       2014-11-07   100
3 2014-11-07 100
2 2014-11-07 0
4 2014-11-06 100
5 2014-11-06 0
6 2014-11-05 100
9 2014-11-05 100
8 2014-11-05 0
7 2014-11-05 100

所以我试图按发布日期对表中的数据进行排序,并且到目前为止我得到的每一天始终将发布商值保留在顶部:

select * from articles 
order by publisher DESC, date DESC

我得到这个结果:

1       2014-11-07   100
3 2014-11-07 100
5 2014-11-06 100
6 2014-11-05 100
9 2014-11-05 100
2 2014-11-07 0
4 2014-11-06 0
7 2014-11-05 0
8 2014-11-05 0

哪个是错误的......

最佳答案

您必须反转 order by 字段的顺序,如下所示:

select * from articles 
order by date DESC, publisher DESC

ORDER BY 子句负责字段的顺序。在此查询中,我们告诉您:按降序排列的日期,如果两个或多个日期相等,则按降序对发布者应用进一步的顺序。

编辑正如我所 promise 的,请访问 Sql Fiddle

关于php - Mysql 按两列排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26912453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com