I asked the question about multiple rows in a previous thread, but now I need to know about a single row. This is an example query:
SELECT * FROM table1 WHERE this = 5 LIMIT 1
But if there is no rows found then I would it to then find the record with a different value:
SELECT * FROM table1 WHERE this = 1 LIMIT 1
So what I want is, find a row with this having the value of 5, if not found then default to finding a row with a value of 1. How do I do that in a single query? Would an OR statement work like so?
SELECT * FROM table1 WHERE this = 5 OR this = 1 LIMIT 1
Would the order of the OR statement try to find any row with the value of 5, if found then it will stop and return the row? IF not found then look for 1 and then return that row? If not then how do I do this with a single query where it searches for one value, if not found defaults to find another value?
I was thinking this query:
SELECT * FROM table1 WHERE this IN(5,1) ORDER BY this ASC LIMIT 1
However this query would always return the value with 1 if both values were found right? So I tried this:
SELECT * FROM table1 WHERE this IN(5,1) ORDER BY this DESC LIMIT 1
But this would always return the value of 5 if both rows were found correct?
Your question appears to prioritize the search for 5 and only if not found then search for 1. Hence your second solution fits the requirement fine. It will return the value of 5 irrespective of if 5 is only found or both 5 and 1 exist.
the 2nd option