Tarang
Tarang

Reputation: 75965

MySQL Maximum value

have a couple of dates in a few fields, basically

SELECT MAX(MAX(DATE_A),MAX(DATE_B)) from table

DATE_A and DATE_B are dates, i basically want the maximum of the latest date of either date A or date B

I searched online but max as a keyword makes it very hard to find what im looking for

Upvotes: 1

Views: 1661

Answers (3)

mathematical.coffee
mathematical.coffee

Reputation: 56935

SELECT MAX(GREATEST(date_a,date_b)) FROM table

This first uses GREATEST(date_a,date_b) to reduce these two columns into just one, containing the greatest value for each row.

The MAX then selects the one maximum value over all of these.

It also prevents repeated evaluation of MAX to make things a littler more efficient.

GREATEST documentation, MAX documentation.

Note that GREATEST operates across columns, whereas MAX operates across rows, which is why you need to use them both to get the single maximum value across both rows and columns.

Upvotes: 3

Nicola Cossu
Nicola Cossu

Reputation: 56397

select greatest(date_a,date_b) as recent from table

Upvotes: 2

silly
silly

Reputation: 7887

try this

SELECT IF(MAX(DATE_A)>MAX(DATE_B),MAX(DATE_A), MAX(DATE_B)) FROM table

Upvotes: 0

Related Questions