Bug #4405 | Strange behaviour on HAVING clause | ||
---|---|---|---|
Submitted: | 5 Jul 2004 4:24 | Modified: | 5 Jul 2004 10:09 |
Reporter: | Quentin Ochem | Email Updates: | |
Status: | Not a Bug | Impact on me: | |
Category: | MySQL Server | Severity: | S1 (Critical) |
Version: | 4.1.2-alpha | OS: | Windows (windows 2000) |
Assigned to: | CPU Architecture: | Any |
[5 Jul 2004 4:24]
Quentin Ochem
[5 Jul 2004 10:09]
Alexander Keremidarski
Sorry, but the bug system is not the appropriate forum for asking support questions. Your problem is not the result of a bug. For a list of more appropriate places to ask for help using MySQL products, please visit http://www.mysql.com/support/ Thank you for your interest in MySQL. Additional info: You a interpretattion of HAVING clause is correct, but you are using agrregate function in wrong way. "I should get at least one result ... where Val is the max" Not at all. MAX() can't be used such way simply because HAVING does comparison row by row (like WHERE clause) and MAX() is undefined before the whole table is considered. Without GROUP BY clause HAVING is same as WHERE with the only difference that HAVING can only reference columns in result set. So in a way SELECT * FROM a HAVING <expr>; is equivalent to SELECT * FROM a WHERE <expr>; But if you try: SELECT * FROM a WHERE MAX(Val) = Val; MySQL will report: ERROR 1111 (HY000): Invalid use of group function However using aggregation functions is allowed in HAVING clause when there is GROUP BY like in: SELECT `Key`, MAX(Val) FROM a group by `key` HAVING MAX(Val) > 80; What you try to achieve is doable with subquery: SELECT * FROM a WHERE Val = (SELECT MAX(Val) FROM a); +-----+-----+ | Key | Val | +-----+-----+ | 2 | 125 | +-----+-----+