Francis Carayol
Francis Carayol

Reputation: 13

How do i select the most recent datetime from 2 tables

I've searched around but can't find the answer I'm looking for.

I have two tables, each has a date field called last_update_date. I want to search which of the two tables has the most recent date and only return that date.

I tried this query hoping it will order the two tables last_update_date field and return that result but the query does not work. Any help would be appreciated.

"Select last_update_date from Table1, Table2 order by last_update_date DESC Limit 1"

Upvotes: 0

Views: 149

Answers (3)

Russell Fox
Russell Fox

Reputation: 5445

SELECT MAX(last_update_date)
FROM (
    SELECT MAX(last_update_date) as last_update_date
    FROM Table1
    UNION ALL
    SELECT MAX(last_update_date)
    FROM Table2
) tMax

Upvotes: 2

cdaiga
cdaiga

Reputation: 4937

SELECT MAX(A.last_update_date) 
 (SELECT last_update_date FROM Table1
  UNION ALL
  SELECT last_update_date FROM Table2) A;

Upvotes: 0

BrunoMarques
BrunoMarques

Reputation: 557

select * from 
( select last_update_date from table1 
  UNION ALL 
  select last_update_date from table2 
) order by last_update_date;

Upvotes: 0

Related Questions