SQL:首先按table.column ='matchingstring'排序,然后按字母顺序排序其他所有内容?

时间:2013-12-17 16:48:08

标签: sql

我目前正在使用MySQL,我想订购book_versions首先显示book_versions.name = 'paperback'的记录,然后显示其余的book_versions(book_versions.name != 'paperback')。我该如何做到这一点?

2 个答案:

答案 0 :(得分:2)

order by case when book_versions.name = 'paperback' then 0 else 1 end,
         book_versions.name, -- remove this line if other names should not be ordered 
         book_versions.isbn 

请参阅sqlFiddle以查看差异

在mysql中,您也可以使用field

order by field(book_versions.name, 'paperback') DESC,
         book_versions.name ASC,
         book_versions.isbn ASC

答案 1 :(得分:1)

尝试:

ORDER BY
    CASE WHEN book_versions.name = 'paperback' THEN 0 ELSE 1 END, -- puts paperbacks first (because paperbacks cause this to =0, which is ordered before 1)
    book_versions.name                                            -- then order alphabetically