Read more

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

Henning Koch
March 07, 2011Software engineer at makandra GmbH

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;
Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

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;
Posted by Henning Koch to makandra dev (2011-03-07 17:32)