[MDEV-26763] GROUPBY IFNULL - column not accessible in HAVING Created: 2021-10-04 Updated: 2021-10-13 Resolved: 2021-10-13 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | Server |
| Affects Version/s: | 10.5.11, 10.6.4 |
| Fix Version/s: | N/A |
| Type: | Bug | Priority: | Major |
| Reporter: | Marek Šrom | Assignee: | Sergei Golubchik |
| Resolution: | Not a Bug | Votes: | 0 |
| Labels: | None | ||
| Environment: |
Cent OS 7, Windows |
||
| Description |
|
When IFNULL is used in GROUPBY, Unknown column error is thrown when trying to use it in HAVING.... To reproduce: SELECT 0 FROM aaaa GROUP BY IFNULL(a,b) HAVING IFNULL( aaaa.a, aaaa.b)<>SUM( IFNULL( aaaa.c, 0)); Result: Error - Unknown column 'aaaa.a' in 'having clause' Strange is that when SELECT * is used works ok... |
| Comments |
| Comment by Alice Sherepa [ 2021-10-05 ] | ||
|
"The SQL standard requires that HAVING must reference only columns in the GROUP BY clause or columns used in aggregate functions. However, MySQL supports an extension to this behavior, and permits HAVING to refer to columns in the SELECT list and columns in outer subqueries as well." | ||
| Comment by Marek Šrom [ 2021-10-05 ] | ||
|
Thanks for your answer, but IFNULL( aaaa.a, aaaa.b) is in GROUP BY and when used in HAVING error occurs... | ||
| Comment by Sergei Golubchik [ 2021-10-13 ] | ||
|
The error is Unknown column 'aaaa.a' in 'having clause', the server does not do subexpression matching. You do not group by a, that's why you have this error. As a workaround you can try to group by IFNULL(a,b),a,b. | ||
| Comment by Marek Šrom [ 2021-10-13 ] | ||
|
Of course I know, that I can add a,b to GROUP BY - in this case IFNULL(a,b) is useless there... However this is not a syntax issue, grouping by a,b can cause different query result: with group by ifnull(a,b) -> result is groupped into 1 row This makes impossible to use non-trivial expressions in group by to be used in having!! Please reconsider this behavior... Thanks | ||
| Comment by Sergei Golubchik [ 2021-10-13 ] | ||
|
To use non-trivial expressions in group by and in having, you can use select aliases:
I agree that a database, technically, should be able to figure it out on its own without hints like aliases. But it is not a bug that it does not do it, it's a feature that wasn't implemented yet. | ||
| Comment by Marek Šrom [ 2021-10-13 ] | ||
|
Thanks for your answer, ok, this works, however it is not a system solution... |