Skip to content
Advertisement

How can I use WHERE with multiple different columns?

Having trouble trying to query AND and OR in an SQlite3 statement.

Is there a way where I can do something like

WHERE section = ? AND LEVEL =? AND subj1 =? AND subj2=? AND subj3 =?

From what I see in this particular line

It won’t work because after ‘AND subj1 =?’ SQlite3 now chooses to select whatever subject that subj2 and subj3 has, completely disregarding the section and level column. How can I get the rows that have match the section, level and subjects that I want?

Here’s my code

Here’s a sample table


Advertisement

Answer

OR has lower logical precedence than AND. To do what you want, you need to surround the ORed predicates with parentheses:

However, here, it is simpler to use IN:

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