Bug #96579 | Unexpected error message when using an outer reference in HAVING | ||
---|---|---|---|
Submitted: | 18 Aug 2019 19:59 | Modified: | 19 Aug 2019 22:27 |
Reporter: | Igor Babaev | Email Updates: | |
Status: | Not a Bug | Impact on me: | |
Category: | MySQL Server: Errors | Severity: | S3 (Non-critical) |
Version: | 5.6/5.7/8.0 | OS: | Any |
Assigned to: | CPU Architecture: | Any |
[18 Aug 2019 19:59]
Igor Babaev
[19 Aug 2019 0:17]
MySQL Verification Team
Thank you for the bug report.
[19 Aug 2019 11:30]
Martin Hansson
Posted by developer: This is not a bug. In the first query, select a from t1 where t1.a=t1.b group by a having exists (select * from t2 where t1.b=t2.d); The HAVING clause is applied on the table produced by the GROUP BY clause. That table contains a single column, a, and hence the inner query refers to a column t1.b that does not exist. In the second query, the aggregated table does contain columns a and b. This is usually not allowed under the sql mode only_full_group_by, because b is neither aggregated (i.e. in the GROUP BY clause) nor the argument of a set function (such as SUM, AVG, MAX, MIN, etc.) But here the optimizer realized that b is functionally dependent on a, via the condition t1.a=t1.b. In the last query, this condition is removed, hence the error. Hope this helps.
[19 Aug 2019 20:37]
Igor Babaev
Martin, Let's a simpler query: mysql> select a from t1 where t1.a=t1.b group by a having t1.b > t2.d; ERROR 1054 (42S22): Unknown column 't1.b' in 'having clause' SQL Standard says: <having clause> ::= HAVING <search condition> Syntax Rules: ... 3) Each column reference directly contained in the <search condition> shall be one of the following: a) A unambiguous reference to a column that is functionally dependent on column references in GROUP BY clause. ... t1.b is functionally dependent on t1.a (due to the equality in the WHERE clause), t1.a is contained in the GROUP BY clause.
[19 Aug 2019 22:27]
Igor Babaev
Martin, In my previous comment I wanted to enter: mysql> select a from t1 where t1.a=t1.b group by a having t1.b > 1; ERROR 1054 (42S22): Unknown column 't1.b' in 'having clause'