yalpsid eman
yalpsid eman

Reputation: 3432

How to select the row with the most recent of 2 date fields?

I have a table that looks, for example, like this:

ID create status update
1 2020-09-07 0 2022-09-07
2 2020-03-25 0 2022-09-07
3 2021-06-12 1 2022-09-07
4 2018-10-12 1 2019-09-07

I'm trying to select the row that has status = 1 and the most recent date in create or update. I know based on this answer how to do this with one date field (ie, selecting the most recent create field alone using Max(create)). But how can one choose the most recent of either? What would this query look like?

Upvotes: 2

Views: 64

Answers (1)

Bill Karwin
Bill Karwin

Reputation: 562250

SELECT ...
FROM a_table_that_looks_for_example_like_this
WHERE status = 1
ORDER BY GREATEST(`create`, `update`) DESC
LIMIT 1

Upvotes: 4

Related Questions