我想通过CustomerID订购,也想订购ORDER BY 2国家/地区,
我有
SELECT CustomerID, Country
FROM Customer
ORDER BY Country IN ("USA", "CANADA") ASC
ORDER BY Customer ID ASC;
应该返回
4, Canada
7, USA
8, USA
11, USA
12, Brazil
13, Belgium
答案 0 :(得分:1)
在CASE
子句中使用ORDER BY
表达式:
SELECT CustomerID, Country
FROM Customer
ORDER BY
CASE WHEN Country IN ("USA", "CANADA") THEN 0 ELSE 1 END,
CustomerID;
这将首先将所有客户置于美国或加拿大,其次是来自所有其他国家/地区的客户。在这两组中的每一组中,记录将按CustomerID
升序排序。