问题
Since mysql does not enforce the Single-Value Rule
(See: https://stackoverflow.com/a/1646121/1688441) does a derived table with an order by guarantee which row values will be displayed? This is for columns not in an aggregate function and not in the group by
.
I was looking at the question (MySQL GROUP BY behavior) after having commented on and answered the question (https://stackoverflow.com/a/24653572/1688441) .
I don't agree with the accepted answer, but realized that a possible improved upon answer would be:
SELECT * FROM
(SELECT * FROM tbl order by timestamp) as tb2
GROUP BY userID;
http://sqlfiddle.com/#!2/4b475/18
Is this correct though or will mysql still decide arbitrarily which row values will be displayed?
回答1:
This query:
SELECT *
FROM (SELECT * FROM tbl order by timestamp) as tb2
GROUP BY userID;
Relies on a MySQL group by extension, which is documented here. You are specifically relying on the fact that all the columns come from the same row and the first one encountered. MySQL specifically warns against making this assumption:
MySQL extends the use of GROUP BY so that the select list can refer to nonaggregated columns not named in the GROUP BY clause. This means that the preceding query is legal in MySQL. You can use this feature to get better performance by avoiding unnecessary column sorting and grouping. However, this is useful primarily when all values in each nonaggregated column not named in the GROUP BY are the same for each group. The server is free to choose any value from each group, so unless they are the same, the values chosen are indeterminate.
So, you cannot depend on this behavior. It is easy enough to work around. Here is an example query:
select t.*
from tbl t
where not exists (select 1 from tbl t2 where t2.userid = t.userid and t2.timestamp > t.timestamp)
With an index on tbl(userid, timestamp)
this may even work faster. MySQL does a notoriously poor job of optimizing aggregations.
来源:https://stackoverflow.com/questions/24728091/mysql-group-by-behavior-when-using-a-derived-table-with-order-by