按 table 中的多个列值排序
Sort by multiple column values in a table
我有一批 table 格式如下:
Shipment
Source
Destination
shipment 1
Spain
France
shipment 2
Landon
Germany
shipment 3
Netherlands
Sweden
shipment 4
Finland
France
shipment 6
Landon
Belgium
shipment 7
Landon
France
shipment 8
Germany
France
shipment 9
Landon
France
shipment 10
Landon
France
shipment 11
Germany
France
我如何对上面的 table 进行排序,首先显示所有德国到法国,然后是兰登到法国,然后是兰登到德国,然后是剩余的货物。
Shipment
Source
Destination
shipment 11
Germany
France
shipment 8
Germany
France
shipment 7
Landon
France
shipment 9
Landon
France
shipment 10
Landon
France
shipment 2
Landon
Germany
shipment 1
Spain
France
shipment 3
Netherlands
Sweden
shipment 4
Finland
France
shipment 6
Landon
Belgium
感谢您的帮助!
这是在 ORDER BY
子句中使用 CASE
语句的想法,正如评论中已经建议的那样:
SELECT
*
FROM
Shipments
ORDER BY
CASE WHEN `Source` = 'Germany' AND `Destination` = 'France' THEN 1
WHEN `Source` = 'Landon' AND `Destination` = 'France' THEN 2
WHEN `Source` = 'Landon' AND `Destination` = 'Germany' THEN 3
ELSE 4
END
这里还有一个 fiddle:https://www.db-fiddle.com/f/58b3pWrUAeobtNd7yyUwwq/0。
我有一批 table 格式如下:
Shipment | Source | Destination |
---|---|---|
shipment 1 | Spain | France |
shipment 2 | Landon | Germany |
shipment 3 | Netherlands | Sweden |
shipment 4 | Finland | France |
shipment 6 | Landon | Belgium |
shipment 7 | Landon | France |
shipment 8 | Germany | France |
shipment 9 | Landon | France |
shipment 10 | Landon | France |
shipment 11 | Germany | France |
我如何对上面的 table 进行排序,首先显示所有德国到法国,然后是兰登到法国,然后是兰登到德国,然后是剩余的货物。
Shipment | Source | Destination |
---|---|---|
shipment 11 | Germany | France |
shipment 8 | Germany | France |
shipment 7 | Landon | France |
shipment 9 | Landon | France |
shipment 10 | Landon | France |
shipment 2 | Landon | Germany |
shipment 1 | Spain | France |
shipment 3 | Netherlands | Sweden |
shipment 4 | Finland | France |
shipment 6 | Landon | Belgium |
感谢您的帮助!
这是在 ORDER BY
子句中使用 CASE
语句的想法,正如评论中已经建议的那样:
SELECT
*
FROM
Shipments
ORDER BY
CASE WHEN `Source` = 'Germany' AND `Destination` = 'France' THEN 1
WHEN `Source` = 'Landon' AND `Destination` = 'France' THEN 2
WHEN `Source` = 'Landon' AND `Destination` = 'Germany' THEN 3
ELSE 4
END
这里还有一个 fiddle:https://www.db-fiddle.com/f/58b3pWrUAeobtNd7yyUwwq/0。