If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > SELECT rows in multiple tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-10-05, 06:21
mg999 mg999 is offline
Registered User
 
Join Date: Dec 2005
Posts: 2
SELECT rows in multiple tables

Hi,

My Web host is still using MySQL 4.0.26.

I have a question where I'd like to create a selection of people in my persons table by searching in a categories table where if I select 2 categories, all the people found must have both selected.

In the categories table, I have a cat_personsID field that is related to the person_ID field.

When I do the following:

SELECT persons,categories WHERE person_ID = cat_personsID AND (cat_catID = 3 OR cat_catID = 7)

I'm able to get all the records with either 3 or 7, but I want to get all the people have both categories selected so that every person has a categories record with 3 and a categories record with 7. I tried with AND, but of course, that won't work...I get NO records at all. And I do have records with both 3 and 7 selected.

I'd be grateful for any help! :-)

Take care,
Mimi.
Reply With Quote
  #2 (permalink)  
Old 12-10-05, 06:33
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
select person_ID
  from persons
inner
  join categories 
    on person_ID = cat_personsID 
 where cat_catID in ( 3 , 7 )
group
    by person_ID
having count(*) = 2
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 12-10-05, 06:51
mg999 mg999 is offline
Registered User
 
Join Date: Dec 2005
Posts: 2
Thank you, thank you, thank you!!!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On