I answered a this question: https://stackoverflow.com/a/18521684/1707323 with a query similiar to:
SELECT
*
FROM
(
SELECT
*
FROM
table_name
WHERE
table_name.some_field='1' OR
table_name.some_field='2'
ORDER BY
table_name.some_field
) sub_query
GROUP BY
sub_query.primary_key_column
And the comment was made
Clever idea, but this is not guaranteed to work in MySQL, and will cause a hard error in SQL Server and most other database systems. Quoting from Extensions to GROUP BY: "The server is free to choose any value from each group, so unless they are the same, the values chosen are indeterminate. Furthermore, the selection of values from each group cannot be influenced by adding an ORDER BY clause.
It's entirely possible that for your specific version of MySQL, for your specific tables, it will always give you the results you want, because the query plan you get causes your query to be executed exactly the way you expect. But if the documentation explicitly states that it will indeterminately choose a value, and that your ORDER BY is not enough to make this query reliable, I would be very cautious about trying this on other databases, or other versions of MySQL.
The MySQl manual continues also to say: Sorting of the result set occurs after values have been chosen, and ORDER BY does not affect which values within each group the server chooses.
Which would be true for queries such as
SELECT
*
FROM
table_name
WHERE
table_name.some_field='1' OR
table_name.some_field='2'
GROUP BY
table_name.primary_key_column
ORDER BY
table_name.some_field
But that is different than the query with the order by clause in the sub query. I did find similar question, but yet again it does not involve the ORDER BY clause in the sub query
Can anyone shed some light on this in reference to the MySQL manual whether an ORDER BY
clause in the subquery will force grouping to always use the first instance of that group.
EDIT: Observations
Shouldn't FIFO or LIFO have something to do with GROUP BY with the way it operates? I just can't wrap my mind around:
The server is free to choose any value from each group
There has to be some sort of logic to this choice. The computer is not allowed to make choices on its own without some sort of directive. If the computer can make choices on its own without any directive then we have truly reached the ultimate goal in computing, ARTIFICIAL INTELLIGENCE, and now the computers can write all my code by giving them one directive for the application. [LOL] What are the odds that this is undocumented logic? wouldn't RAND in the logic take more processing power than FIFO or FILO? There has to be some logic involved here that may just be undocumented but works 100% of the time. One thing I always loved is proofs in Geometry. I learned that you have to prove if it is right or wrong. I can prove how it works, yet I have not seen how it does not work. Prove how it is so if it works that way or prove how it is not so if it does not work that way.