|
<div id="page" class="sidebar">
<div class="section">
clause cannot refer to nonaggregated columns in the select list that are not named in theclause. For example,this query is illegal in standard SQL because thecolumn in the select list does not appear in the:
For the query to be legal,thecolumn must be omitted from the select list or named in theclause.
so that the select list can refer to nonaggregated columns not named in theclause. 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 theare 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. Furthermore,the selection of values from each group cannot be influenced by adding anclause. Sorting of the result set occurs after values have been chosen,anddoes not affect which values within each group the server chooses.
A similar MySQL extension applies to theclause. In standard SQL,a query that includes aclause cannot refer to nonaggregated columns in theclause that are not named in theclause. A MySQL extension permits references to such columns to simplify calculations. This extension assumes that the nongrouped columns will have the same group-wise values. Otherwise,the result is indeterminate.
To disable the MySQLextension,enable theSQL mode. This enables standard SQL behavior: Columns not named in theclause cannot be used in the select list orclause unless enclosed in an aggregate function.
also affects use of aliases in theclauses. For example,the following query returnsvalues that occur only once in table:
MySQL extends this behavior to permit the use of an alias in theclause for the aggregated column:
Enablingdisables this MySQL extension and aerror occurs because the columnin theclause is not enclosed in an aggregate function (instead,itisan aggregate function).
The select list extension also applies to. That is,you can refer to nonaggregated columns in theclause that do not appear in theclause. (However,as mentioned previously,does not affect which values are chosen from nonaggregated columns; it only sorts them after they have been chosen.) This extension does not apply if theSQL mode is enabled.
In some cases,you can useandto obtain a specific column value even if it is not unique. If thecolumn contains integers no larger than 6 digits,the following query gives the value offrom the row containing the smallestvalue:
See.
If you are trying to follow standard SQL,you cannot use expressions inclauses. As a workaround,use an alias for the expression:
tbl_name
GROUP BY id,val;
MySQL permits expressions inclauses,so the alias is unnecessary:
tbl_name
GROUP BY id,FLOOR(value/100);
http://dev.mysql.com/doc/refman/5.0/en/group-by-handling.html (编辑:安卓应用网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|