简体   繁体   English

MySQL按两列排序

[英]Mysql Order by two columns

I have a table like this: 我有一张这样的桌子:

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

I am trying to get a result like this : 我试图得到这样的结果:

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

SO I am trying to sort the data in table by publish date and allways keep the publisher values on top for each day I got this so far: 因此,我试图按发布日期对表中的数据进行排序,并始终将发布者的价值保持在到目前为止的每一天:

select * from articles 
order by publisher DESC, date DESC

I get this result: 我得到这个结果:

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

Which is wrong.... 哪有错...

You must invert the order of order by field, like this: 您必须按字段反转订单顺序,如下所示:

select * from articles 
order by date DESC, publisher DESC

The ORDER BY clause take care about order of field. ORDER BY子句负责字段的顺序。 In this query we tell: order by date in desc order and if two or more dates are equals, apply a further order on publisher in desc order. 在此查询中,我们说:按日期顺序按desc顺序排序,如果两个或多个日期相等,则按desc顺序对发布者应用另一个顺序。

EDIT As I promised, go to Sql Fiddle 编辑,如我所愿,请转到Sql Fiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM