Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Mysql query returns ID that I'd like to hide?

    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?

  • #2
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I think in sql != doesnt work try using NOT IN or something like that

  • #3
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    use <> not !=, thats in javascript

    http://www.sqlcourse.com awesome tutorial

  • #4
    New Coder
    Join Date
    Oct 2011
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thanks for the replies guys, but the id still shows up...

  • #5
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    SELECT * FROM videos WHERE (tags LIKE '%federer%' OR tags LIKE '%gasquet%' OR tags LIKE '%paris%') AND id <> '2'

    give this a shot

  • Users who have thanked DanInMa for this post:

    Krtoffel (11-13-2011)

  • #6
    New Coder
    Join Date
    Oct 2011
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts
    That did the trick, thanks a lot!

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    The reason:

    Harken back to elementary school days.

    What is the value of
    Code:
        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
    Code:
        ( 3 + 4 ) * 2
    OR is like +
    AND is like *

    When you do
    Code:
         a or b or c and d
    you are, effectively, writing
    Code:
         a or b or ( c and d )
    So, as Dan said, you need to explicitly use the parentheses:
    Code:
        ( 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/...ator_not-equal
    Last edited by Old Pedant; 11-14-2011 at 12:08 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Krtoffel (11-17-2011)

  • #8
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    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.

  • Users who have thanked webdev1958 for this post:

    Krtoffel (11-17-2011)

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by webdev1958 View Post
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    it's the way I was taught and I agree with the principle

  • #11
    New Coder
    Join Date
    Oct 2011
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thanks guys, I'll keep that in mind


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •