How to get max date from two columns, or null if either column is null?
I have two date columns in a MySQL table representing tasks that are worked on by two users. The date columns record when user 1 agreed to the task and when user 2 agreed to the task, and can be
NULL if that user hasn't agreed to the task yet.
I want to generate the date task agreed - which should be NULL unless both users have agreed to the task, or the maximum of the two dates if they've both agreed.
So I want a query that produces:
user_1_agreed user_2_agreed query_result NULL NULL NULL 2014-01-02 NULL NULL NULL 2014-03-04 NULL 2014-01-02 2014-03-04 2014-03-04
I'm aware of
IF, but I'm having trouble trying to work out how to combine them to give the above.
greatest function fits your bill perfectly:
SELECT GREATEST(user_1_agreed, user_2_agreed) FROM my_table
- A Linux, Apache, MySQL, PHP (LAMP) stack is a combination of open source software that is typically installed together to enable a server to host dynamic […]
- MySQL is an the most popular open-source database management system, in most of hosting company its has been bundle by LAMP(Linux, Apache, MySQL, PHP/Python/Perl) stack. Another alternative […]
- MySQL is the world’s most popular open-source database. Despite its powerful features, MySQL is simple to set up and easy to use. Below are some instructions […]