0

MySQL version 8.0

Given a table:

id      date1      date2
1      2020-01-01  2020-01-02
2      2020-03-03  2020-05-02
3      2020-04-04  2020-02-11

I would like to create additional column that chooses min(date1, date2).

I know min(date1) = column-wise earliest date but how can I do row-wise minimum operation on dates?

Desired output should look like this:

id      date1      date2         min(date)
1      2020-01-01  2020-01-02    2020-01-01
2      2020-03-03  2020-05-02    2020-03-03
3      2020-04-04  2020-02-11    2020-02-01

Thanks in advance!

haneulkim
  • 4,406
  • 9
  • 38
  • 80
  • 1
    answering your own question is fine, but don't put the answer at the bottom of the question, enter it as "Your Answer" below – ysth Sep 02 '20 at 03:13

1 Answers1

0

Answer: MySQL Select minimum/maximum among two (or more) given values

Simply use least function -> id, date1, date2, least(date1,date2)

I had to search for a while to find the answer so I am hoping this would lead to answer quicker.

haneulkim
  • 4,406
  • 9
  • 38
  • 80