MySQL query results are sorted by a certain value

Source: Internet
Author: User

How can I sort MySQL query results? This is a question raised by many people. The following describes how to sort MySQL query results by a certain value. If you are interested, take a look.

Previously, a function was modified, and MySQL query results were required:

Id name ***

1 lucy...

3 lucy...

2 lily...

4 lucy...

The name of lucy is at the top of the list, which cannot be understood. Some people may say that simple union or a temporary table or something. In fact, I also thought about it, however, there are many SQL logic in itself. The above is just a simple example. If union is used, or temporary tables may be a great detour. Later I saw an article trying to add order by find_in_set (name, 'Lucy '), and the result is lucy all below. Then I change it to order by find_in_set (name, 'Lucy') desc and the result is

Id name ***

1 lucy...

3 lucy...

4 lucy...

2 lily...

Basic implementation, but a little uncertain mood, query mysql documentation found find_in_set syntax

 
 
  1. FIND_IN_SET(str,strlist)   
  2.  

If the string str is in the strlist of the string column data table consisting of N substrings, the return value ranges from 1 to N. A data table in a string column is a string consisting of several self-chains separated by the "," symbol. If the first parameter is a constant string and the second parameter is the type SET column, the FIND_IN_SET () function is optimized and computed in bits. If str is not in strlist or strlist is a null string, the return value is 0. If any parameter is NULL, the return value is NULL. This function cannot run properly when the first parameter contains a comma (,).

 
 
  1. mysql> SELECT FIND_IN_SET('b','a,b,c,d');  
  2.  
  3.         -> 2  
  4.  

After reading this, why do I add desc to my estimation? The find_in_set returned value is: when lucy exists, return its position. If not, it is 0. If it is null, It is null, so the sorting is,. If it is added to the column

Id name FIND_IN_SET **

1 lucy 1...

3 lucy 1...

2 lily 0...

4 lucy 1...

The table structure is as follows:

 
 
  1. mysql> select * from test;  
  2. +----+-------+  
  3. | id | name   |  
  4. +----+-------+  
  5. |   1 | test1 |  
  6. |   2 | test2 |  
  7. |   3 | test3 |  
  8. |   4 | test4 |  
  9. |   5 | test5 |  
  10. +----+-------+  

Run the following SQL:

 
 
  1. mysql> select * from test where id in(3,1,5);  
  2. +----+-------+  
  3. | id | name   |  
  4. +----+-------+  
  5. |   1 | test1 |  
  6. |   3 | test3 |  
  7. |   5 | test5 |  
  8. +----+-------+  
  9. 3 rows in set (0.00 sec)  

The results of this select statement in mysql are automatically sorted in ascending order of IDs,
However, if I want to execute "select * from test where id in (, 5);", the results are sorted by the conditions in, namely, 3, 1, 5. The expected results are as follows:
Id name
3 test3
1 test1
5 test5

The method is as follows:

 
 
  1. select * from test where id in(3,1,5) order by find_in_set(id,'3,1,5');  
  2. select * from test where id in(3,1,5) order by substring_index('3,1,2',id,1);  

Both

Use functions to query row numbers in MySQL

Non-empty question in MySQL Query

MySQL Date and Time Functions

Two common MySql query time period methods

In-depth parsing of MySQL query Cache Mechanism

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.