Link to home
Create AccountLog in
Avatar of Sirdots
Sirdots

asked on

Using 'And' to get result of matching data in sql table

I have a sql table with two records. Please look at the attached Image.

I want to be able to test every field for a particular value. If I run the query below

select value1,value2,value3 from tester2
where value1 like  '%'+ 'aaa' + '%' or value2 like  '%'+ 'aaa' + '%' or value3 like  '%'+ 'aaa' + '%'
And (value1 like  '%'+ 'bbb' + '%' or value2 like  '%'+ 'bbb' + '%' or value3 like  '%'+ 'bbb' + '%')

My query returns

   value1   value2    value3

1  aaa        null          null


This is wrong because  I only have one entry of aaa and another of bbb. I dont have a situation where both aaa and bbb exists for a record. How can I correct this?
If i use or instead of the And, it works very well.  

User generated image
Avatar of Ephraim Wangoya
Ephraim Wangoya
Flag of United States of America image


You must use OR

using AND implies that the a filed must satisfy both conditions ie (id like 'aaa' and id like 'bbb') which is not possible

The only time and would be true in your case is if you had a record with id 'aaabbb'
Then in this case both conditions match this value Like %aaa% and Like %bbb%
Avatar of Sirdots
Sirdots

ASKER

I want to be able to use OR and And. How can i do this? OR works very well for me.
ASKER CERTIFIED SOLUTION
Avatar of AriMc
AriMc
Flag of Finland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Sirdots

ASKER

Thanks. Very helpful and works for me.