My table has some TIMESTAMP
columns, for which I am running a query to get the most recent one as a Unix timestamp. Suppose all the columns are zeroed (they are default '0'
), i.e. I didn't set the value yet.
- If I select each column with
unix_timestamp(col)
, it will return0
for each column:
select unix_timestamp(`last_ip_ok`), unix_timestamp(`last_ip_error`), unix_timestamp(`last_ownership_ok`), unix_timestamp(`last_ownership_error`) from `websites`;
+------------------------------+---------------------------------+-------------------------------------+----------------------------------------+
| unix_timestamp(`last_ip_ok`) | unix_timestamp(`last_ip_error`) | unix_timestamp(`last_ownership_ok`) | unix_timestamp(`last_ownership_error`) |
+------------------------------+---------------------------------+-------------------------------------+----------------------------------------+
| 0 | 0 | 0 | 0 |
+------------------------------+---------------------------------+-------------------------------------+----------------------------------------+
- If I select the
greatest
of all columns, it will return the zero timestamp, that is,0000-00-00 00:00:00
:
select greatest(`last_ip_ok`, `last_ip_error`, `last_ownership_ok`, `last_ownership_error`) from `websites`;
+--------------------------------------------------------------------------------------+
| greatest(`last_ip_ok`, `last_ip_error`, `last_ownership_ok`, `last_ownership_error`) |
+--------------------------------------------------------------------------------------+
| 0000-00-00 00:00:00 |
+--------------------------------------------------------------------------------------+
- However, if I combine the functions and run
unix_timestamp(greatest(cols...))
, it just returnsnull
:
select unix_timestamp(greatest(`last_ip_ok`, `last_ip_error`, `last_ownership_ok`, `last_ownership_error`)) from `websites`;
+------------------------------------------------------------------------------------------------------+
| unix_timestamp(greatest(`last_ip_ok`, `last_ip_error`, `last_ownership_ok`, `last_ownership_error`)) |
+------------------------------------------------------------------------------------------------------+
| <null> |
+------------------------------------------------------------------------------------------------------+
This is unexpected, I expected the return value to be 0
since it should be equivalent to unix_timestamp('0000-00-00 00:00:00')
, which is the return value of greatest(cols...)
. Is this correct, or is it possibly a bug?