Tomazi
Tomazi

Reputation: 847

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....

Upvotes: 0

Views: 54

Answers (1)

Joe Taras
Joe Taras

Reputation: 15379

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. 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.

EDIT As I promised, go to Sql Fiddle

Upvotes: 1

Related Questions