Function to return the minimum or maximum value per row with MySQL

Updated . Posted . Visible to the public.

MySQL's MIN and MAX functions are for aggregations only. This will not work and produce an error:

SELECT id, MIN(birthday, '1978-01-01') FROM users;

In order to compute the minimum or maximum value for the current row, use LEAST Show archive.org snapshot and GREATEST Show archive.org snapshot instead:

SELECT id, LEAST(birthday, '1978-01-01') FROM users;
Henning Koch
Last edit
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2011-03-07 16:32)