Tutte queste query forniranno tutti un errore di sintassi su qualsiasi piattaforma SQL conforme agli standard SQL.
SELECT * FROM plant ORDER BY SUM(id);
SELECT * FROM plant ORDER BY COUNT(fruit);
SELECT * FROM plant ORDER BY COUNT(*);
SELECT * FROM plant ORDER BY SUM(1) DESC;
Su PostgreSQL, ad esempio, tutte le query generano lo stesso errore.
ERROR: column "plant.id" must appear in the GROUP BY clause or be used in an aggregate function
Ciò significa che stai utilizzando una funzione di aggregazione del dominio senza utilizzare GROUP BY. SQL Server e Oracle restituiscono messaggi di errore simili.
MySQL's GROUP BY è noto per essere rotto in diversi aspetti, almeno per quanto riguarda il comportamento standard. Ma le domande che hai postato sono state un nuovo comportamento interrotto, quindi +1 per quello.
Invece di cercare di capire cosa sta facendo sotto il cofano, probabilmente stai meglio imparando a scrivere query GROUP BY standard. MySQL corrisponderà alle istruzioni standard GROUP BY dello per quanto ne so.
Le versioni precedenti di documenti MySQL ti avvisavano di GROUP BY e colonne nascoste. (Non ho un punto di riferimento, ma questo testo è citato in tutto il luogo.)
Do not use this feature if the columns you omit from the GROUP BY part are not constant in the group. The server is free to return any value from the group, so the results are indeterminate unless all values are the same.
More recent versions are a little different.
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.
Personalmente, non ritengo indeterminata una funzionalità di SQL.
Ottima spiegazione, grazie! – nawfal