How consider NULL as the MAX date instead of ignoring it in MySQL? How consider NULL as the MAX date instead of ignoring it in MySQL? mysql mysql

How consider NULL as the MAX date instead of ignoring it in MySQL?


Give this a shot:

SELECT ID, case when MAX(DATE IS NULL) = 0 THEN max(DATE) END AS DATEFROM testGROUP BY ID;


Null is not a value or a number it's just null. This is why you use "where col1 is null" and not "col1 = null". The workaround is to use IFNULL and set a really high value.

select ID, max(IFNULL(DATE,'3000-01-01'))from testgroup by ID


FYI NULL is ignored when used in aggregation function.

mysql> SELECT * FROM null_test;+------+| c    |+------+| NULL ||    1 ||    2 ||    3 |+------+4 rows in set (0.00 sec)mysql> SELECT COUNT(c) FROM null_test;+----------+| COUNT(c) |+----------+|        3 | <= not 4 but 3+----------+1 row in set (0.00 sec)mysql> SELECT COUNT(*) FROM null_test;+----------+| COUNT(*) |+----------+|        4 | <= in this case not 3 but 4+----------+1 row in set (0.00 sec)mysql> SELECT AVG(c) FROM null_test;+--------+| AVG(c) |+--------+| 2.0000 | <= not (1+2+3) / 4 but (1+2+3) / 3+--------+1 row in set (0.00 sec)