对一个字段进行排序,然后对另一个字段进行排序

时间:2015-12-03 13:49:17

标签: sql

我有以下SQL:

SELECT * 
FROM forum_l.entries 
WHERE bindningstid = 1 
ORDER BY 
    CASE 
       WHEN rabatt IS NULL THEN 1 
       ELSE 0 
    END, rabatt DESC, 
    CASE 
        WHEN paslag IS NULL THEN 1 
        ELSE 0 
    END, paslag ASC

所以我首先得到一个值(rabatt),我想订购desc,然后我想订购字段paslag Asc但仍然保持null。

1 个答案:

答案 0 :(得分:2)

你应该替换案件:

SELECT * 
FROM forum_l.entries 
WHERE bindningstid = 1 
ORDER BY 
    CASE 
       WHEN rabatt IS NULL THEN 0
       ELSE 1 
    END, rabatt DESC, 
    CASE 
        WHEN paslag IS NULL THEN 0 
        ELSE 1 
    END, paslag ASC