Jump to content

Select with both ANDs and ORs


430 Man

Recommended Posts

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? ;-)

Link to comment
https://forums.phpfreaks.com/topic/233625-select-with-both-ands-and-ors/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.