Mysql - Capture default entries if specific entries arent found

Using MySQL and php, how could I get all the results in one query that match a particular query, but if the results are not found, it finds all the results by default? For example, I have this query:

SELECT * FROM table1 WHERE typeid = 5

If the results are not found from the query below, I want to find all the results from table1 with type 1:

SELECT * FROM table1 WHERE typeid = 1

How can I do this in a single request? If I try the following query, I get both 5 and 1:

SELECT * FROM table1 WHERE typeid = 5 OR typeid = 1

All I want is a query table1 for all records assigned using typeid of 5, if no results are found, then all results are assigned typeid = 1 in one query. How can i do this? Or should I have 2 requests?

EDIT

I need this for multiple returned records, not a single record.

+3
3
SELECT *
FROM   table1
WHERE  typeid = IF((SELECT COUNT(*) FROM table1 WHERE typeid = 5), 1, 5);
+2

?

SELECT * FROM table1
WHERE CASE WHEN (SELECT COUNT(1) FROM table1 WHERE typeid = 5) > 0 THEN
    typeid = 5
ELSE
    typeid = 1
END;
+5
    SELECT * 
    FROM table1 
    WHERE typeid = 5
  UNION ALL
    SELECT * 
    FROM table1 
    WHERE typeid = 1
      AND NOT EXISTS
          ( SELECT * 
            FROM table1 
            WHERE typeid = 5
          ) ;
0

All Articles