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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.