MySQL numeric string type field sorting problem

Source: Internet
Author: User
Tags abs numeric numeric value


Today to ask the net to add a list of functions, the main thing is to list the most read and commented articles in this blog, but there is a problem, the reading volume stored field type is varchar, use DESC or ASC sort, not according to the size of the number of the order, Instead, sort by the numbers in the string, and then share the two field-sorting problems that solve the MySQL number string type.
Field *1 or +0

*1 a field or +0 to sort the MySQL string field by numeric value

SELECT * FROM table where 1 order by id*1 Desc;

Or

SELECT * FROM table where 1 order by id+0 Desc;

In fact, the field is treated as an int type.
Use Find_in_set () for unbeatable ordering

Find_in_set (str,strlist);

Returns a value if string str is in a list of n substrings, Strlist returns an 1 to N. A list of strings is made up of multiple substrings separated by the character ",". If the first argument is a constant string, and the second argument is a set column type, the Find_in_set () function is optimized to use bit operations!
If STR is not strlist or if Strlist is an empty string, the return value is 0. If any one argument is null, the return value is also null. If the first argument contains a "," This function will not work at all:

Select P.*, Find_in_set (p.products_id, $string _hot_pid) as rank from products p where p.products_id in ($string _hot_pid) or Der by rank


When the actual operation is found to use the numeric value of the character to sort out the output, 2 rows after the number of all the first character 1

SQL is as follows:

SELECT DISTINCT WEEK from Bk_cmsindex WHERE termid= ' & Termid & ' ORDER by WEEK

How to do it, the method is to *1 or +0 of the numeric field.

SELECT DISTINCT WEEK from Bk_cmsindex WHERE termid= ' & Termid & ' ORDER by week*1

Or

SELECT DISTINCT WEEK from Bk_cmsindex WHERE termid= ' & Termid & ' ORDER by week+0

Add

First, the use of MySQL cast function, conversion field format

Here we convert the Meta_value field to the field decimal of the numeric type, and then sort:


Order by CAST (' Meta_value ' as DECIMAL)

It is important to note that the value of the Meta_value field you want to sort must all be convertible to a number, otherwise there will be an error.

Second, use the MySQL absolute value function abs

Using the MySQL absolute value function abs, it tells MySQL to use absolute value to handle this field:

Order by ABS (' Meta_value ')

Related Article

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.