MySQL Select another row if one doesn't exist MySQL Select another row if one doesn't exist mysql mysql

MySQL Select another row if one doesn't exist


Try this:

SELECT * FROM    (SELECT * FROM your_table      WHERE id = your_id      LIMIT 1    UNION    SELECT * FROM your_table      LIMIT 1) aLIMIT 1

The idea is to take first desired row and appending to this very first row, finally taking first one. If desired row does not exists, first one will be selected...


SELECT * FROM (   SELECT * FROM table1 WHERE [..your criteria...] LIMIT 1   UNION   SELECT * FROM table1 LIMIT 1    ) LIMIT 1