如何使用MySQL按特定顺序排序行?

时间:2014-04-30 04:23:12

标签: mysql sql

我有这样的查询:

SELECT BookSize(page_count) as Size, count(*) as NmbrBoooks 
    FROM books 
    WHERE page_count IS NOT NULL 
        AND page_count > 0 
    GROUP BY Size;

输出如下:

+-----------+------------+
| Size      | NmbrBoooks |
+-----------+------------+
| ExtraLong |          3 |
| Long      |         11 |
| Medium    |         51 |
| Short     |         27 |
+-----------+------------+

我想根据书的长度对此进行排序,因此我从上到下依次排列以下行:Short,Medium,Long,ExtraLong。

实现这一目标的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

试试这个:

select BookSize(page_count) as Size, count(*) as NmbrBoooks from books
where page_count is not null and page_count > 0 group by Size order by page_count;