Since I'm a newbie, I cannot post useful images, so I will try to do this with text ...
I just tested this, and it seems that the values of fields that are NOT in GROUP BY will use the values of the FIRST string, which matches the group by condition. It will also explain the perceived “randomness” that others have experienced when choosing columns that are not part of the group by clause.
Example:
Create a table called "test" with two columns named "col1" and "col2" with data that looks like this:
Col1 col2
12
12
thirteen
2 1
2 2
2 3
3 1
3 2
3 3
Then run the following query:
select col1, col2
col2 desc
You will get this result:
thirteen
2 3
3 3
12
12
2 2
3 2
2 1
3 1
Now consider the following query:
select groupTable.col1, groupTable.col2
from (
select col1, col2
from the test
col2 order desc
) groupTable
group by groupTable.col1
order by groupTable.col1 desc
You will get this result:
3 3
2 3
thirteen
Change the subquery to asc:
select col1, col2
from the test
order by col2 asc
Result:
2 1
3 1
12
12
2 2
3 2
thirteen
2 3
3 3
Use this again as the basis for your subquery:
select groupTable.col1, groupTable.col2
from (
select col1, col2
from the test
col2 asc order
) groupTable
group by groupTable.col1
order by groupTable.col1 desc
Result:
3 1
2 1
12
Now you should be able to see how the order of the subquery affects what values are selected for the selected fields, but not in the group by section. This explains the perceived “randomness” that others talked about, because if the subquery (or is missing there) is not combined with the ORDER BY clause, then mysql will capture the rows as they arrive, but having determined the sort order in the subquery that you can control this behavior and get predictable results.