开发者

Mysql sort and sort again, but this does not work ORDER BY date DESC, view DESC

开发者 https://www.devze.com 2023-03-28 23:30 出处:网络
I\'ve got a table with 400 entry, I do a first sort to get the latest entry and limit 20 I endup with a output limited to the latest 20 entry, order by date

I've got a table with 400 entry, I do a first sort to get the latest entry and limit 20

I endup with a output limited to the latest 20 entry, order by date

SELECT date,view FROM `blog` order by date DESC limit 20

    date          view
    2011/08/16    18
    2011/08/15    134
    2011/08/15    20
    2011/08/13    800
开发者_开发百科    ... 

But now I would like to sort again this output, if I try to add another sort, nothing change

SELECT date,view FROM `blog` order by date DESC, view DESC limit 20


    date          view
    2011/08/16    18
    2011/08/15    134
    2011/08/15    20
    2011/08/13    800

I would like to have

date          view
2011/08/13    800
2011/08/15    134
2011/08/15    20
2011/08/16    18

thx for your help!!!

Do I need to create a tmp table and resort this table, or is there a cleaver way of doing it?


This should work; you just select from the first result set.

SELECT x.date, x.view FROM (SELECT date,view FROM `blog` order by date DESC limit 20) as x order by view DESC

What is does: it first selects the last 20 records from blog. It uses the results as input for the next SELECT that orders it descending based on view.

0

精彩评论

暂无评论...
验证码 换一张
取 消