MySQL order by skills

Source: Internet
Author: User

From: http://blog.chinaunix.net/uid-7692530-id-2567549.html

 

1. sort by date only, ignore year
> Select date, description
From table_name order
Month (date), dayofmonth (date );
Note: Using dayofyear alone will cause errors, such as the same level as the 3-1 day of the non-leap year.

 

2. Sorting point fractional IP Address
> Select IP from table_name order
Inet_aton (IP );
Or int unsigned is used to represent the IP address when designing the table.

 

3. Sort the special values in a column to the top
For example, if you want to display the names of Lulu in the table at the top of the list
> Select name from table_name order by if (name = 'lulu',), name
;
You can also replace the if condition with the corresponding statement as needed.

 

4. Sort the content of a column in the User-Defined order.
For example, if you want to sort the table names by Lulu, XiXi, Baba, and mama in an unconventional order:
> Select name from table_name order
Field (name, 'lulu', 'xixi', 'baba ', 'Mama ');

 

5. Sort enumeration types
By default, the enumeration type is Numeric sorting. If you want to sort the data in string format.
> Select name from table_name order by cast (name
As char );
If you want to change the default order of Enum sorting, you can alter table
Alter table table_name modify name
Enum ('lulu', 'xixi', 'Mama', 'baba ');

 


6. sort by a string of the CSV type
For example, if the content of a column m_str is in the form of a abc-321-mno-jkl, you want to sort the second column
> Select m_str from table_name
Order by substring_index (m_str, '-', 2), '-',-1 );

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.