430 Man Posted April 13, 2011 Share Posted April 13, 2011 I searched around but I have not been able to find how to do this... Consider this dataset swiped from php.net: mysql> select * from ourpets; +----------+--------+------------+------+------------+------------+ | name | owner | species | sex | birth | death | +----------+--------+------------+------+------------+------------+ | Fluffy | Harold | cat | f | 1993-02-04 | NULL | | Claws | Gwen | cat | m | 1994-03-17 | NULL | | Buffy | Harold | dog | f | 1989-05-13 | NULL | | Fang | Thomas | dog | m | 1990-08-27 | NULL |* | Bowser | Amanda | dog | m | 1979-08-31 | 1995-07-29 | | Chirpy | Gwen | bird | f | 1998-09-11 | NULL | | Whistler | Gwen | bird | f | 1997-12-09 | NULL | | Slim | Benny | snake | m | 1996-04-29 | NULL | | Dalli | Alli | canine | m | 2001-12-20 | NULL | | Tara | Thomas | canine | m | 2002-05-17 | NULL |* | Mimi | Alli | guinea pig | m | 2004-05-17 | NULL | | Buffy | Joe | dog | f | 1989-05-13 | NULL | | Fang | Thomas | dog | m | 1990-08-27 | NULL |* | Bowser | Evan | dog | m | 1979-08-31 | 1995-07-29 | | Chirpy | Gwen | bird | f | 1998-09-11 | NULL | | Whistler | Gwen | bird | f | 1997-12-09 | NULL | | Slim | Benny | snake | m | 1996-04-29 | NULL | | Dalli | Amanda | canine | m | 2001-12-20 | NULL | | Tara | Thomas | canine | f | 2002-05-17 | NULL | | Mimi | Alli | guinea pig | m | 2004-05-17 | NULL | +----------+--------+------------+------+------------+------------+ 11 rows in set (0.00 sec) Given the above db, I'm trying to set up a Select that will find all dogs or canines that are both owned by Thomas and are male. (should be 3 results that I marked with *) I tried Select * from ourpets where species='dog' OR species='canine' AND owner='Thomas' AND sex='male'; But that gives me all dogs plus all the canines. (10 results) Anybody know how to do this correctly? ;-) Quote Link to comment https://forums.phpfreaks.com/topic/233625-select-with-both-ands-and-ors/ Share on other sites More sharing options...
ignace Posted April 13, 2011 Share Posted April 13, 2011 SELECT * FROM ourpets WHERE (species='dog' OR species='canine') AND owner='Thomas' AND sex='male'; 0 OR 1 = 1 1 OR 1 = 1 0 XOR 1 = 1 1 XOR 1 = 0 0 AND 1 = 0 1 AND 1 = 1 Quote Link to comment https://forums.phpfreaks.com/topic/233625-select-with-both-ands-and-ors/#findComment-1201229 Share on other sites More sharing options...
430 Man Posted April 13, 2011 Author Share Posted April 13, 2011 Well golly, I tried parentheses several times but it did not work... guess I had a typo somewhere or something. sigh but thanks Quote Link to comment https://forums.phpfreaks.com/topic/233625-select-with-both-ands-and-ors/#findComment-1201236 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.