-
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.
-
SELECT * FROM `events` WHERE `forid` = '0' OR `forid` = '1'
-
which is same as
SELECT * FROM events WHERE forid in ('0','1')
-
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.
-
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
-
Forum Rules
|
|