MySQL Choose minimum / maximum from two (or more) given values

Can SELECT set the minimum or maximum value of two or more values. I need something like this:

 SELECT MAX_VALUE(A.date0, B.date0) AS date0, MIN_VALUE(A.date1, B.date1) AS date1 FROM A, B WHERE Bx = Ax 

Can I achieve this using MySQL?

+72
sql mysql
Oct 18 '13 at 9:12
source share
3 answers

To achieve this, you can use the LEAST and GREATEST functions.

 SELECT GREATEST(A.date0, B.date0) AS date0, LEAST(A.date1, B.date1) AS date1 FROM A, B WHERE Bx = Ax 

Both are described here http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html

+144
Oct 18 '13 at 9:16
source share

I assume you are looking for:

GREATEST()

and

LEAST()

+23
Oct 18 '13 at 9:17
source share

Try the following:

 SELECT GREATEST(A.date0, B.date0) AS `date0`,LEAST(A.date0, B.date0) AS `date1` FROM A JOIN B ON A.id = B.role; 
+1
Oct 18 '13 at 9:34 on
source share



All Articles