Bug #10973 Having clause fails when using a unique column
Submitted: 30 May 2005 20:21 Modified: 6 Jun 2005 15:37
Reporter: Peter Gulutzan Email Updates:
Status: Can't repeat Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S3 (Non-critical)
Version:5.0.7-beta-debug OS:Linux (SUSE 9.2)
Assigned to: Igor Babaev CPU Architecture:Any

[30 May 2005 20:21] Peter Gulutzan
Description:
The expression "HAVING column_name = value" returns zero rows,
even though the expression is true.
Or it returns the wrong row (perhaps always the first row in the table?).
This happens only if I create the table with "UNIQUE (column_name)" --
if I don't, the HAVING succeeds.

How to repeat:
mysql> create table td (s1 decimal(1), unique (s1));
Query OK, 0 rows affected (0.00 sec)

mysql> insert into td values (null),(1),(2);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select s1, avg(s1) from td group by s1 having s1 = 1;
+------+---------+
| s1   | avg(s1) |
+------+---------+
| NULL | NULL    |
+------+---------+
1 row in set (0.08 sec)

mysql> delete from td where s1 is null;
Query OK, 1 row affected (0.02 sec)

mysql> select s1, avg(s1) from td group by s1 having s1 = 1;
Empty set (0.00 sec)

mysql> drop table td;
Query OK, 0 rows affected (0.00 sec)

mysql> create table td (s1 decimal(1));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into td values (null),(1),(2);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select s1, avg(s1) from td group by s1 having s1 = 1;
+------+---------+
| s1   | avg(s1) |
+------+---------+
| 1    | 1.0000  |
+------+---------+
1 row in set (0.01 sec)
[30 May 2005 20:27] MySQL Verification Team
Thank you for the bug report.