Click here to Skip to main content
15,124,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an issue here. I have an SQLite command here where my "WHERE" doesn't work as intended. I am new to SQLite in general and it's the first time I work a lot with "AND", "OR" and "LIKE".

I am trying to understand why Num1 and the other integer value are not working.


"SELECT * FROM tbl_Device 
WHERE  Num1 = '1' AND Num2 = '2' AND Num3 = '3' 
AND Flag LIKE 'LastVersion' OR  Flag LIKE 'OnlyVersion'"



So here I am suppose to only get a Num1 of 1, Num2 of 2 and Num3 of 3, but I also get something that I didn'ty wanted: Num1 of 5, Num2 of 3 and Num3 of 4


I don't know where I am wrong here, so I thought I could use some help.

What I have tried:

"SELECT * FROM tbl_Device 
WHERE  Num1 = '1' AND Num2 = '2' AND Num3 = '3' 
AND Flag LIKE 'LastVersion' OR  Flag LIKE 'OnlyVersion'"
Posted
Updated 2-Mar-21 8:30am
v2

1 solution

When mixing 'and' and 'or' you must use brackets to show what you really mean e.g.

SQL
SELECT * FROM tbl_Device 
WHERE  
(Num1 = '1' AND Num2 = '2' AND Num3 = '3' )
AND 
( Flag LIKE 'LastVersion' OR  Flag LIKE 'OnlyVersion' )
As an aside there is no point in using 'LIKE' unless you are using a wildcard e.g.
SQL
LIKE 'LastVersion%'

You probably wanted to use
SQL
AND Flag IN ('LastVersion','OnlyVersion')
in which case you wouldn't have hit the and/or problem
   
Comments
Etienne-Perinet 2-Mar-21 13:51pm
   
I am actually trying this out, but yeah this code looks way better than what I was going for.
Etienne-Perinet 2-Mar-21 14:23pm
   
It works!

Thank you very much for your time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900