MySQL sort null value

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.