Re:Multiple values in SELECT query

From: Date: Mon, 10 Mar 2008 20:40:12 +0000
Subject: Re:Multiple values in SELECT query
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-44737@lists.php.net to get a copy of this message
At 19:34 09/03/2008, you wrote:
From: Ron Piggott <ron.php@actsministries.org> Date: Sun, 09 Mar 2008 15:34:05 -0400 Message-Id: <1205091245.16466.1.camel@localhost.localdomain> 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

« previous php.db (#44737) next »