www.webdeveloper.com
Results 1 to 3 of 3

Thread: sql: and ... or ...

  1. #1
    Join Date
    Apr 2005
    Posts
    210

    Exclamation sql: and ... or ...

    following sql is just meaning - wrong in syntax:

    select * from ... where user_id <> 10 and (name='hello' or name='good');

    brace above makes "or" has higher priority than "and" - similar to 5x(2+3) in math.

    do I have to write user_id <> 10 for twice in real sql as bellow?

    select * from ... where user_id <> 10 and name='hello' or user_id <> 10 and name='good';

    Any comments to make the sql simpler?

    Thanks

  2. #2
    Join Date
    Feb 2006
    Posts
    520
    select * from ... where user_id <> 10 and (name='hello' or name='good'); is as simple as it gets. Another way of doing it would be:

    select * from ... where (user_id <> 10 and name='hello') or (user_id <> 10 and name='good');

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,918
    Another option which might become useful if the list of possible values for "name" increases:
    Code:
    SELECT * FROM table_name WHERE user_id <> 10 AND name NOT IN('hello', 'good', 'foo', 'bar')
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles