0

Long story short, I'm trying to get the time my site was last updated. The main content of my site is stored in four tables. Each of these tables has an updated_at field.

I'm able to combine them in a nested query:

SELECT (
    SELECT updated_at FROM proposition ORDER BY updated_at DESC LIMIT 1
) as latest_proposition,
(
    SELECT updated_at FROM product ORDER BY updated_at DESC LIMIT 1
) as latest_product,
(
    SELECT updated_at FROM review ORDER BY updated_at DESC LIMIT 1
) as latest_review,
(
    SELECT updated_at FROM segment ORDER BY updated_at DESC LIMIT 1 
) as latest_segment

..which gives me something like..


-------------------+-------------------+-------------------+-------------------
latest_proposition |latest_product     |latest_review      |latest_segment     
-------------------+-------------------+-------------------+-------------------
2011-05-25 14:45:30|2011-05-25 14:37:28|2011-05-12 09:20:19|2011-05-12 09:20:19
-------------------+-------------------+-------------------+-------------------
1 row in set (0.00 sec)

My question is - How do I now get the latest of these fields? To get the overall "site last updated" time.

Thanks in advance!

1
  • 1
    I think you are after a union. Commented May 26, 2011 at 10:43

4 Answers 4

4
select
    max(updated_at) as updated_at
from
(
    SELECT max(updated_at) as updated_at FROM proposition
    UNION
    SELECT max(updated_at) FROM product
    UNION
    SELECT max(updated_at) FROM review
    UNION
    SELECT max(updated_at) FROM segment
) x
Sign up to request clarification or add additional context in comments.

2 Comments

@GolexTrol, use a UNION ALL otherwise MySQL will (try to) eliminate duplicate rows which will slow the query down.
Yes, thats true. Through in in this, trying to eliminate duplicates from total of 4 will not be that much a hit. Besides, less rows, will speed up retrieving the max in the main query. (all theoretically when dealing with just 4 rows). But a good point indeed. UNION ALL is usually better and hardly ever worse than UNION.
3

Use GREATEST() on the returned fields (after passing them through COALESCE(), if you expect NULL values).

Comments

0
SELECT updated_at 
from 
(
    SELECT updated_at FROM proposition ORDER BY updated_at DESC LIMIT 1
          union all
    SELECT updated_at FROM product ORDER BY updated_at DESC LIMIT 1
          union all
    SELECT updated_at FROM review ORDER BY updated_at DESC LIMIT 1
          union all
    SELECT updated_at FROM segment ORDER BY updated_at DESC LIMIT 1 
) x
order by updated_at limit 1

Comments

0
SELECT MAX(updated_at)
FROM (SELECT MAX(updated_at) FROM proposition UNION ALL
  SELECT MAX(updated_at) FROM product UNION ALL
  SELECT MAX(updated_at) FROM review UNION ALL
  SELECT MAX(updated_at) FROM segment) x

1 Comment

You'll need to alias the field in the subselect. As I did ;*)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.