I was wondering if it is possible to write a SQL SELECT query with searching precedence (I have looked at the typical precedence, AND before OR etc but that doesn’t do what I want it do).
Below is a basic query that is basis for the overall query:
SELECT TOP 15 * FROM MyTable WHERE reference LIKE '123%' OR first_name LIKE 'Pa%';
P.S. reference is not the PK, its just a value associated with a person
Now what I am hoping someone could help me with is, I want it first to fill up the top 15 with those that match ‘123%’ and then what ever space is left over with the those that match the first_name ‘Pa%’. If I run this query in my code it fills the TOP 15 with those that match ‘Pa%’ now I am assuming it is because of the way it searches through the database (it finds 15 matches to ‘Pa%’ before it matches the reference), so is there a way to tell the query to first look for ‘123%’ in reference then when its found or not found any values search ‘Pa%’ in first_name or would I have to write to separate query and merge there results?
~Thank you in advance 😀
Or even simpler:
truenaturally orders afterfalse, so just order by the match you want + DESC