I have a shipment table in the following format:
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 |
How I can sort the above table with all the Germany to France appear first, then Landon to France followed by Landon to Germany, then the remaining shipments.
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 |
Thanks for the help!
CodePudding user response:
Here's the idea of using a CASE
statement inside the ORDER BY
clause, as already suggested in the comments:
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
Here's a fiddle too: https://www.db-fiddle.com/f/58b3pWrUAeobtNd7yyUwwq/0.