Mysql UNION Tables MAX date

mysql

Solution

If you want the greatest value from `MAX(date_due)` and `MAX(date_start)`

add an alias to your MAX function

use an UNION between your two queries

use that UNIONed query as a subquery.

select id, MAX(mx) FROM

(SELECT id, MAX(date_due) as mx FROM tasks WHERE parent_id = '35' AND date_due > CURDATE()
UNION
SELECT id, MAX(date_start) as mx FROM calls WHERE parent_id = '35' AND date_start > CURDATE()) s

Problem

I have two tables calls and tasks. I want to get the farthest date which ever is from both tables. The result will be one date that is the Maximum of all. So basically want to join the quires below SELECT `id`, MAX(`date_due`) FROM tasks WHERE `parent_id` = '35' AND date_due > CURDATE() SELECT `id`, MAX(`date_start`) FROM calls WHERE `parent_id` = '35' AND date_start > CURDATE() These will result the max date from each table but how do i get single record which is the Highest Date.

Original source