Skip to content
Advertisement

SQL WHERE statement returning both or conditions

I’m trying to return a single row based on this where statement

The issue is I have to get a row based on multiple criteria and the more that match determines the final match. The statement above returns a row just fine if it matches the bottom or statement, but if it matches the first OR it returns results for both statements.

LIMIT 1 doesn’t work either as sometimes it gives preference to the wrong result.

EDIT:

community_id service_type_id unit_class_id
1 1 1
1 1 null

Because of the way the table is both rows are true, my understanding was SQL took the first one that was true and returned it.

I apologize for not a lot of info I was hoping maybe there was just a bit of info I was missing. Here is my query.

Advertisement

Answer

I’m assuming that you want to get the record that matches the most conditions first. One way to do that is to order by the number of matching conditions (in this case only one condition is different):

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement