Results 1 to 5 of 5

Thread: Query same column twice

  1. #1
    Join Date
    Apr 2007
    Posts
    2

    Query same column twice

    Hi, I'm pretty sure that I've missed something obvious but I can't think of what it is, I need to get rows where "forid" is 0 and 1 but I can't think of how to do it, I tried:

    SELECT * FROM `events` WHERE `forid` = '0' AND `forid` = '1'

    But that query isn't correct.

    Appreciate any help, thanks.

  2. #2
    Join Date
    Sep 2002
    Posts
    5,938
    SELECT * FROM `events` WHERE `forid` = '0' OR `forid` = '1'

  3. #3
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    which is same as

    SELECT * FROM events WHERE forid in ('0','1')

  4. #4
    Join Date
    Apr 2007
    Posts
    1

    Thumbs up More explanation: AND vs. OR

    I just wanted to add to the explanation. Your original query,
    PHP Code:
    WHERE forid '0' AND forid '1' 
    attempts to find a row in your table where the forid column is at the same time 0 AND 1 - this is an impossibility, because the column can contain a 0 or a 1, but not both at the same time, so the query will never return any rows.

    This is a common mistake to make, because you are thinking "I want all rows with 0 and all rows with 1" so you think that you should use the "AND" operator. Instead, think of it as "I want all rows with either a 0 or a 1" and use the "OR" operator.

    Remember that the restrictions in the WHERE clause apply to each row, so think of the query walking down each row in the table and applying the WHERE clause individually to each row.

  5. #5
    Join Date
    Apr 2007
    Posts
    2
    Thanks for the help. I can understand why to use the 'or' operator instead now.

    Thanks again, matt.

Posting Permissions

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