Krtoffel
11-13-2011, 08:09 PM
I'm not really sure what I'm doing wrong here... This is my query:
SELECT * FROM videos WHERE tags LIKE '%federer%' OR tags LIKE '%gasquet%' OR tags LIKE '%paris%' AND id != '2'
Why does it return a result where the id is 2?
AsinNetworks
11-13-2011, 08:20 PM
I think in sql != doesnt work try using NOT IN or something like that
DanInMa
11-13-2011, 08:23 PM
use <> not !=, thats in javascript
http://www.sqlcourse.com awesome tutorial
Krtoffel
11-13-2011, 08:30 PM
Thanks for the replies guys, but the id still shows up... :confused:
DanInMa
11-13-2011, 08:38 PM
SELECT * FROM videos WHERE (tags LIKE '%federer%' OR tags LIKE '%gasquet%' OR tags LIKE '%paris%') AND id <> '2'
give this a shot
Krtoffel
11-13-2011, 08:44 PM
That did the trick, thanks a lot!
Old Pedant
11-13-2011, 11:26 PM
The reason:
Harken back to elementary school days.
What is the value of
3 + 4 * 2
??
If you said 14, go back to sixth grade.
Multiplication has higher *precedence* than addition, so the answer is 11. (Multiply 4 times 2 first *then* add 3.)
Same with AND and OR in all computer languages.
If you wanted 14 as the answer, you needed to write
( 3 + 4 ) * 2
OR is like +
AND is like *
When you do
a or b or c and d
you are, effectively, writing
a or b or ( c and d )
So, as Dan said, you need to explicitly use the parentheses:
( a or b or c ) and d
Oh, and MySQL allows *either* <> or != and treats them 100% the same.
http://dev.mysql.com/doc/refman/5.5/en/comparison-operators.html#operator_not-equal
webdev1958
11-14-2011, 05:47 AM
If in doubt over execution precedence, it's a good idea imo to use brackets even if not technically needed. This makes it totally clear to whoever might read your code.
Old Pedant
11-14-2011, 09:11 AM
If in doubt over execution precedence, it's a good idea imo to use brackets even if not technically needed. This makes it totally clear to whoever might read your code.
Dammed straight! Some places I have worked, you'd get hammered in a code review if you omitted them, even if they weren't needed.
webdev1958
11-15-2011, 12:34 AM
it's the way I was taught and I agree with the principle :)
Krtoffel
11-17-2011, 02:44 PM
Thanks guys, I'll keep that in mind :)