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 3 of 3
  1. #1
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts

    query syntax and conditions

    Hi
    just curious, about a few things, I am going over my query's
    and changing || to OR adding brackets, stuff like that.

    I know its best to enclose different conditions from reading up
    whether you need too or not, separating "OR, AND" etc, just curious
    should I put both in brackets when they exist? or one over another?

    also should I always do AND conditions in the beginning no matter what?
    or are they ok at the end if using brackets?

    also is it good practice to use single quotes like '$var' in query, double or
    none at all.

    I'm strictly doing query's inside scripts only.

    Thanks as always
    Sonny

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,547
    Thanks
    77
    Thanked 4,382 Times in 4,347 Posts
    Too general a question. Depends entirely upon what the WHERE condition is supposed to be testing.

    Code:
    WHERE age >= 18 OR age >= 16 AND parentalConsent = True
    There is an AND condition at the end with no parentheses at all, yet the meaning is perfectly clear:

    It means the same as
    Code:
    WHERE age >= 18 OR ( age >= 16 AND parentalConsent = True )
    *clearly* it would be a mistake to use
    Code:
    WHERE ( age >= 18 OR age >= 16 ) AND parentalConsent = True
    When in doubt, probably use them. But use them correctly.
    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:

    sonny (12-01-2012)

  • #3
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    I did notice a weird result now and then but when I went back and
    added some brackets, that fixed things. I thought using LIKE let me
    have a space before or after a word, without using that %, I really
    do not like using them if I can, I will pay more attention and use =
    whenever I can thanks

    PS "NOT IN" is another thing I need to go back and use now. its
    efficient, I like that.

    Thanks
    Sonny


  •  

    Posting Permissions

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