Description:
When CASE ... WHEN ... END; is used and under some condition it evaluates as NULL all consequent values also become NULL if result is used in
GROUP BY
INSERT ... SELECT
CREATE ... SELECT
while result is Ok otherwise
How to repeat:
create table a (b int);
insert into a values (1), (2), (3);
select b, case b when 2 then NULL else b end as c from a;
+------+------+
| b | c |
+------+------+
| 1 | 1 |
| 2 | NULL |
| 3 | 3 |
+------+------+
Now the BUG:
mysql> select b, case b when 2 then NULL else b end as c from a group by b;
+------+------+
| b | c |
+------+------+
| 1 | 1 |
| 2 | NULL |
| 3 | NULL |
+------+------+
create table d select b, case b when 2 then NULL else b end as c from a;
select * from d;
+------+------+
| b | c |
+------+------+
| 1 | 1 |
| 2 | NULL |
| 3 | NULL |
+------+------+
insert into d select b, case b when 2 then NULL else b end as c from a;
select * from d;
+------+------+
| b | c |
+------+------+
| 1 | 1 |
| 2 | NULL |
| 3 | NULL |
| 1 | 1 |
| 2 | NULL |
| 3 | NULL |
+------+------+
I simplified original report which contained:
SELECT ... MAX(CASE ...) ... GROUP BY ...;
result was wrong because as above shows that after frst NULL CASE evaluates as NULL so
MAX(List_of_NULLs) -> NULL