How does GROUP BY DESC select its order?

前端 未结 2 579
忘掉有多难
忘掉有多难 2021-01-22 19:36

So I am creating sections for a store. The store can have multiple scopes, if there isn\'t a section_identifier set for a given store_id it should fall

相关标签:
2条回答
  • 2021-01-22 20:04

    GROUP BY sorts records in ascending order by default. Your store_id is not being referenced at all instead the records returned are sorted in ascending order of the section_identifier

    0 讨论(0)
  • 2021-01-22 20:06
    SELECT * FROM my_table GROUP BY section_identifier
    

    is an invalid SQL query.

    How GROUP BY works?

    Let's get the query above and see how GROUP BY works. First the database engine selects all the rows that match the WHERE clause. There is no WHERE clause in this query; this means all the rows of the table are used to generate the result set.

    It then groups the rows using the expressions specified in the GROUP BY clause:

    +----+--------------------+----------------------+----------+
    | id | section_identifier | option_identifier    | store_id |
    +----+--------------------+----------------------+----------+
    | 17 | header             | header_option_one    |        1 |
    | 23 | header             | header_option_three  |        0 |
    +----+--------------------+----------------------+----------+
    | 18 | footer             | footer_option_one    |        0 |
    +----+--------------------+----------------------+----------+
    | 19 | homepage_feature   | homepage_feature_one |        0 |
    | 25 | homepage_feature   | homepage_feature_one |        1 |
    +----+--------------------+----------------------+----------+
    

    I marked the groups in the listing above to make everything clear.

    On the next step, from each group the database engine produces a single row. But how?

    The SELECT clause of your query is SELECT *. * stands for the full list of table columns; in this case, SELECT * is a short way to write:

    SELECT id, section_identifier, option_identifier, store_id
    

    Let's analyze the values of column id for the first group. What value should the database engine choose for id? 17 or 23? Why 17 and why 23?

    It does not have any criteria to favor 17 over 23. It just picks one of them (probably 17 but this depends on a lot of internal factors) and goes one.

    There is no problem to determine the value for section_identifier. It is the column used to GROUP BY, all its values in a group are equal.

    The choosing dilemma occurs again on columns option_identifier and store_id.


    According to the standard SQL your query is not valid and it cannot be executed. However, some database engines run it as described above. The values for expressions that are not (at least one of the below):

    • used in the GROUP BY clause;
    • used with GROUP BY aggregate functions in the SELECT clause;
    • functionally dependent of columns used in the GROUP BY clause;

    are indeterminate.

    Since version 5.7.5, MySQL implements functional dependency detection and, by default, it rejects an invalid GROUP BY query like yours.

    How to make it work

    It's not clear for me how you want to get the result set. Anyway, if you want to get some rows from the table then GROUP BY is not the correct way to do it. GROUP BY does not select rows from a table, it generates new values using the values from the table. A row generated by GROUP BY, most of the times, do not match any row from the source table.

    You can find a possible solution to your problem in this answer. You'll have to write the query yourself after you read and understand the idea (and is very clear to you how the "winner" rows should be selected).

    0 讨论(0)
提交回复
热议问题