Re:Multiple values in SELECT query

[email protected] ("Neil Smith [MVP, Digital media]")
Newsgroups php.db
Message-ID <[email protected]>
At 19:34 09/03/2008, you wrote:
>From: Ron Piggott <[email protected]>
>Date: Sun, 09 Mar 2008 15:34:05 -0400
>Message-Id: <[email protected]>
>
>What is the correct syntax for where the results may be 1 or 2?  What
>have I done wrong?
>
>SELECT * FROM table WHERE name LIKE "ABC" AND listing_type = 1 or 2


C'mon Ron, this is basic SQL. The query you provided would have given 
all rows where name was like 'abc', listing type was 1, then returned 
all rows because `OR 2` results in a value of 'true' which matches 
everything (you could also have written OR 2 = 2 with the same effect)

SELECT * FROM table WHERE name LIKE "ABC" AND (listing_type = 1 OR 
listing_type = 2);

SELECT * FROM table WHERE name LIKE "ABC" AND listing_type IN (1, 2);



Now I've spoon fed you, please read the manual on "Operator 
Precedence" which explains how to combine stuff like this so you 
don't get the "wrong" result 
http://dev.mysql.com/doc/refman/5.0/en/operator-precedence.html : "To 
override this order and group terms explicitly, use parentheses" 
(first query above)



Cheers - Neil
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.