Second SELECT query if first SELECT returns 0 rows

One option would be to use UNION ALL with EXISTS:

SELECT * 
FROM proxies 
WHERE A='B'
UNION ALL
SELECT * 
FROM proxies 
WHERE A='C' AND NOT EXISTS (
    SELECT 1
    FROM proxies 
    WHERE A='B'
)

This will return rows from the proxies table where A='B' if they exist. However, if they don’t exist, it will look for those rows with A='C'.

Leave a Comment