mysql having where 别名 相对位置
小结:
1) where having 的过滤范围
having group by 、聚合函数
where select 字段
2)别名支持的位置
-
The alias is used as the expression's column name and can be used in
GROUP BY,ORDER BY, orHAVINGclauses.
3)
a HAVING clause must come after any GROUP BY clause and before any ORDER BY clause.
MySQL :: MySQL 8.0 Reference Manual :: 15.2.13 SELECT Statement https://dev.mysql.com/doc/refman/8.0/en/select.html
-
The
WHEREclause specifies conditions on columns in the select list, but cannot refer to aggregate functions. TheHAVINGclause specifies conditions on groups, typically formed by theGROUP BYclause.
-
GROUP BYpermits aWITH ROLLUPmodifier. See Section 14.19.2, “GROUP BY Modifiers”.Previously, it was not permitted to use
ORDER BYin a query having aWITH ROLLUPmodifier. This restriction is lifted as of MySQL 8.0.12. See Section 14.19.2, “GROUP BY Modifiers”. -
The
HAVINGclause, like theWHEREclause, specifies selection conditions. TheWHEREclause specifies conditions on columns in the select list, but cannot refer to aggregate functions. TheHAVINGclause specifies conditions on groups, typically formed by theGROUP BYclause. The query result includes only groups satisfying theHAVINGconditions. (If noGROUP BYis present, all rows implicitly form a single aggregate group.)The
HAVINGclause is applied nearly last, just before items are sent to the client, with no optimization. (LIMITis applied afterHAVING.)The SQL standard requires that
HAVINGmust reference only columns in theGROUP BYclause or columns used in aggregate functions. However, MySQL supports an extension to this behavior, and permitsHAVINGto refer to columns in theSELECTlist and columns in outer subqueries as well.If the
HAVINGclause refers to a column that is ambiguous, a warning occurs. In the following statement,col2is ambiguous because it is used as both an alias and a column name:Press CTRL+C to copySELECT COUNT(col1) AS col2 FROM t GROUP BY col2 HAVING col2 = 2;Preference is given to standard SQL behavior, so if a
HAVINGcolumn name is used both inGROUP BYand as an aliased column in the select column list, preference is given to the column in theGROUP BYcolumn. -
Do not use
HAVINGfor items that should be in theWHEREclause. For example, do not write the following:Press CTRL+C to copySELECT col_name FROM tbl_name HAVING col_name > 0;Write this instead:
Press CTRL+C to copySELECT col_name FROM tbl_name WHERE col_name > 0; -
The
HAVINGclause can refer to aggregate functions, which theWHEREclause cannot:Press CTRL+C to copySELECT user, MAX(salary) FROM users GROUP BY user HAVING MAX(salary) > 10;(This did not work in some older versions of MySQL.)
-
MySQL permits duplicate column names. That is, there can be more than one
select_exprwith the same name. This is an extension to standard SQL. Because MySQL also permitsGROUP BYandHAVINGto refer toselect_exprvalues, this can result in an ambiguity:Press CTRL+C to copySELECT 12 AS a, a FROM t GROUP BY a;In that statement, both columns have the name
a. To ensure that the correct column is used for grouping, use different names for eachselect_expr. -
The
WINDOWclause, if present, defines named windows that can be referred to by window functions. For details, see Section 14.20.4, “Named Windows”. -
MySQL resolves unqualified column or alias references in
ORDER BYclauses by searching in theselect_exprvalues, then in the columns of the tables in theFROMclause. ForGROUP BYorHAVINGclauses, it searches theFROMclause before searching in theselect_exprvalues. (ForGROUP BYandHAVING, this differs from the pre-MySQL 5.0 behavior that used the same rules as forORDER BY.)

浙公网安备 33010602011771号