Search code examples
mysqlsqlmariadbmariasql

MySQL Select when not exist return 1


I have a problem in the MySQL Query:

How can i SELECT something but when it errors because it doesn´t exist it should return a value like 1 or something else.

My Query

SELECT License FROM fivem_auth WHERE IP = '34.45.34.77';

Solution

  • You can UNION your query with a query that returns the default if the row doesn't exist.

    SELECT License FROM fivem_auth WHERE IP = '34.45.34.77'
    UNION ALL
    SELECT 1 FROM DUAL
    WHERE NOT EXISTS (SELECT 1 FROM fivem_auth WHERE IP = '34.45.34.77')