SQL vs MySQL: Rules about aggregate operations and GROUP BY

前端 未结 4 1717
遥遥无期
遥遥无期 2021-02-14 05:11

In this book I\'m currently reading while following a course on databases, the following example of an illegal query using an aggregate operator is given:

4条回答
  •  长情又很酷
    2021-02-14 05:34

    Based on a link which a_horse_with_no_name provided in a comment, I have arrived at my own answer:

    It seems that the MySQL way of using GROUP BY differs from the SQL way, in order to permit leaving out columns, from the GROUP BY clause, when they are functionally dependant on other included columns anyways.

    Lets say we have a table displaying the activity of a bank account. It's not a very thought-out table, but it's the only one we have, and that will have to do. Instead of keeping track of an amount, we imagine an account starts at '0', and all transactions to it is recorded instead, so the amount is the sum of the transactions. The table could look like this:

    +------------+----------+-------------+
    | costumerID | name     | transaction |
    +------------+----------+-------------+
    |       1337 | h4x0r    |         101 |
    |         42 | John Doe |         500 |
    |       1337 | h4x0r    |        -101 |
    |         42 | John Doe |        -200 |
    |         42 | John Doe |         500 |
    |         42 | John Doe |        -200 |
    +------------+----------+-------------+
    

    It is clear that the 'name' is functionally dependant on the 'costumerID'. (The other way around would also be possible in this example.)

    What if we wanted to know the costumerID, name and current amount of each customer?

    In such a situation, two very similar queries would return the following right result:

    +------------+----------+--------+
    | costumerID | name     | amount |
    +------------+----------+--------+
    |         42 | John Doe |    600 |
    |       1337 | h4x0r    |      0 |
    +------------+----------+--------+
    

    This query can be executed in MySQL, and is legal according to SQL.

    SELECT costumerID, name, SUM(transaction) AS amount
    FROM Activity
    GROUP BY costumerID, name
    

    This query can be executed in MySQL, and is NOT legal according to SQL.

    SELECT costumerID, name, SUM(transaction) AS amount
    FROM Activity
    GROUP BY costumerID
    

    The following line would make the query return and error instead, since it would now have to follow the SQL way of using aggregation operations and GROUP BY:

    SET sql_mode = 'ONLY_FULL_GROUP_BY';
    

    The argument for allowing the second query in MySQL, seems to be that it is assumed that all columns mentioned in SELECT, but not mentioned in GROUP BY, are either used inside an aggregate operation, (the case with 'transaction'), or are functionally dependent on other included columns, (the case with 'name'). In the case of 'name', we can be sure that the correct 'name' is chosen for all group entries, since it is functionally dependant on 'costumerID', and therefore there is only one possibly name for each group of costumerID's.

    This way of using GROUP BY seems flawed tough, since it doesn't do any further checks on what is left out from the GROUP BY clause. People can pick and choose columns from their SELECT statement to put in their GROUP BY clause as they see fit, even if it makes no sense to include or leave out any particular column.

    The Sailor example illustrates this flaw very well. When using aggregation operators (possibly in conjunction with GROUP BY), each group entry in the returned set has only one value for each of its columns. In the case of Sailors, since the GROUP BY clause is left out, the whole table is put into one single group entry. This entry needs a name and a maximum age. Choosing a maximum age for this entry is a no-brainer, since MAX(S.age) only returns one value. In the case of S.sname though, wich is only mentioned in SELECT, there are now as many choices as there are unique sname's in the whole Sailor table, (in this case two, John and Jane Doe). MySQL doens't have any clue which to choose, we didn't give it any, and it didn't hit the brakes in time, so it has to just pick whatever comes first, (Jane Doe). If the two rows were switched, it would actually give "the right answer" by accident. It just seems plain dumb that something like this is allowed in MySQL, that the result of a query using GROUP BY could potententially depend on the ordering of the table, if something is left out in the GROUP BY clause. Apparently, that's just how MySQL rolls. But still couldn't it at least have the courtesy of warning us when it has no clue what it's doing because of a "flawed" query? I mean, sure, if you give the wrong instructions to a program, it probably wouldn't (or shouldn't) do as you want, but if you give unclear instructions, I certainly wouldn't want it to just start guessing or pick whatever comes first... -_-'

提交回复
热议问题