Search code examples
mysqlsqllimit

Limit 1 of each from different column if conditions


Is there a way to say, select 2 rows from the database, where one column equals something, or one column equals something else, but you want one row each from EACH equal conditions? So...

SELECT * FROM tableName WHERE colName = '1' OR colName = '2' LIMIT...

1 where colName = '1', and 1 where colName = '2'. So, one of each.


Solution

  • If you have the values, then use union all:

    (select t.*
     from tablename t
     where colname = '1'
     limit 1)
    union all
    (select t.*
     from tablename t
     where colname = '2'
     limit 1)