Order by multiple fields in Mysql

The work needs to use the last two fields of order by to sort, but the result is a bug, which is for the record.

[1] Reproduce the problem scene

In order to illustrate the problem, simulate the example database table students, the effect is the same as the example.

The following statement Sql_1:

SELECT * FROM students st ORDER BY st.sAge, st.sGrade DESC;

(1) Expected results:

Both sAge and sGrade are sorted in descending order

img

(2) Actual results:

sAge in ascending order, sGrade in descending order

img

(3) Analyze the reasons:

When order by multiple fields, the Sql syntax understanding error causes the query result set to be inconsistent with expectations.

[2] Default ascending order

In MySql, order by is in ascending order by default.

Example statement Sql_2:

 SELECT * FROM students st ORDER BY st.sAge;

Query result set:

img

Obviously, if the sorting method is not written, the default is ascending.

[3] When there are multiple fields, each defines ascending and descending order

In MySql, when order by multiple fields, you need to define the ascending and descending order respectively.

Of course, it is the reason why the bug is caused by not writing the Sql statement according to this syntax at the beginning of this article.

Example statement Sql_3:

SELECT * FROM students st ORDER BY st.sAge DESC, st.sGrade DESC;

Query result set:

img

Obviously, it is wrong for Sql_1 to achieve the result of Sql_3. Because the sorting method is not written, the default is ascending.

[4] When there are multiple fields, prioritize them in order

In MySql, when ordering by multiple fields, the priority is arranged according to the order of the fields.

In the above example, we only used two fields, let's use three fields to verify this rule.

(1) Query by sAge ascending order, sGrade descending order, sStuId descending order

Example statement Sql_4:

 SELECT * FROM students st ORDER BY st.sAge ASC, st.sGrade DESC, st.sStuId DESC;

Query result set:

img

Obviously, after the order of the first two fields is determined, the sStuId value is not arranged in descending order

(2) Query by sAge ascending order, sStuId descending order, sGrade descending order

Example statement Sql_5:

SELECT * FROM students st ORDER BY st.sAge ASC, st.sStuId DESC, st.sGrade DESC;

Query result set:

img

Obviously, after the order of the first two fields is determined, the sGrade value is not arranged in descending order

Compare the difference between Sql_4 and Sql_5 statements, and then compare the query result set, and slowly understand these two differences.

【5】Summary

(1) In MySql, use the ASC or DESC keywords to set the query results in ascending or descending order.

(2) Especially very, very important: Arranged in ascending order (ASC) by default.

(3) Two (or more) fields can be added after order by, and the fields are separated by English commas.

(4) If A uses ascending order and B uses descending order, the SQL should be written like this: order by A ASC, B DESC; the default is the same, you can also write like this: order by A, B DESC;

(5) If both A and B use descending order, two DESC must be used, order by A DESC, B DESC;

(6) When there are multiple fields, the priority is determined in sequence.

order by should be one of the more commonly used methods in the database, the following simple demonstration of the use of order by

One, a single field

    格式:
    select * from product order by price desc
    影响:price 字段会排序,其他字段自然排序

img

Second, multiple fields

  格式:
  select * from product order by 字段A desc,字段B asc
  影响:数据会先按照第一个字段排序(price),如果第一个字段的值相同,再按照第二个字段排序!
   由上图可以知:表里name值为苹果和小米的price值是相同的,但是他们的order_count 值不同,苹果的
   order_count 大于 小米的。这个时候执行2条sql,分别都是price 排降序,    order_count 一条降序,
一条升序

order_count descending order:

     select * from product order by price desc,order_count desc

img

order_count ascending order:

    select * from product order by price desc,order_count asc

img

Comparing two different queries, there are different results, which verifies that our previous inference is correct. The sorting of the second field is only used when the values ​​of the first sorting field are the same. Otherwise the second field has no effect.

Summary When sorting multiple fields in MYSQL, you can set the sorting method for each field. Of course, most of the actual situation is that multiple fields are sorted in a unified way

Guess you like

Origin blog.csdn.net/qq_43842093/article/details/132131124