Starting from the realistic project demand;
There is a city table;
There are Beijing, Shanghai, Guangzhou, Hebei, Tianjin, Henan 6 cities;
Mysql>Select*From Bjy_order;+----+------+| Id| City|+----+------+| 1 | Beijing || 2 | Shanghai || 3 | Guangzhou || 4 | Hebei || 5 | Tianjin || 6 | Henan |+----+------+
Sqlcopy
The requirement is to let Shanghai row the first, Tianjin row second;
The simplest and most brutal method is to add a order_number field;
Used to identify the order;
Then by order by order_number asc sorting
``
Mysql>Select*From Bjy_orderOrderby Order_numberAsc;+----+------+--------------+| Id| City| Order_number|+----+------+--------------+|2| Shanghai|1||5| Tianjin|2|| Beijing | 1 | 3 || 3 guangzhou | 4 || Hebei, 4 | 5 || 6- Henan | 6 | +----+------+--------------+
Sqlcopy
Doing so does satisfy demand;
But what if the table contains all 32 provinces in China?
If you come to a national county and city table hundreds of data?
And we just want to get some values in front of the line;
Just as most people only know that the first peak of the world is Mt. Everest without paying attention to the second third;
The first thing we should think about is to just give the sort numbers that need to be in front, and the other is null;
Mysql>Select*From Bjy_order;+----+------+--------------+| Id| City| Order_number|+----+------+--------------+|1| Beijing|Null||2| Shanghai|1||3 guangzhou | NULL || Hebei, 4 | NULL || Tianjin | 5 | 2 || 6- Henan | NULL | +----+------+--------------+
Sqlcopy
Then we order by a bit;
Mysql>Select*From Bjy_orderOrderby Order_numberAsc;+----+------+--------------+| Id| City| Order_number|+----+------+--------------+|1| Beijing|Null||3| Guangzhou|Null||Hebei, 4 | NULL || 6- Henan | NULL || Shanghai | 2 | 1 || Tianjin | 5 | 2 | +----+------+--------------+
Sqlcopy
But what is frustrating when it is about to succeed;
Those are the null rows at the front;
OK, here is the main character of today to solve this problem;
Let's use is null the SQL for a little makeover.
Mysql>Select*From Bjy_orderOrderby Order_numberIsNull, Order_numberAsc;+----+------+--------------+| Id| City| Order_number|+----+------+--------------+|2| Shanghai|1||5| Tianjin|2||1 | Beijing | null || 3 | Guangzhou | null || 4 | Hebei | null || 6 | Henan | null | +----+------+--------------+
Sqlcopy
To achieve the perfect demand;
MySQL sort null value