Welcome to dbFreaks.com!
FAQFAQ   SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

select where multiple joined records match

 
   Database Help (Home) -> mySQL RSS
Next:  How to get path to a database  
Author Message
anmar

External


Since: Feb 13, 2005
Posts: 1



(Msg. 1) Posted: Sun Feb 13, 2005 12:40 am
Post subject: select where multiple joined records match
Archived from groups: mailing>database>mysql (more info?)

I'm trying to figure out how to select all the records in one table
which have multiple specified records in a second table.

Here's a simplified version of my problem.

I have two tables, resources and goals.

resources table:

ID TITLE
1 civil war women
2 bunnies on the plain
3 North Carolina and WWII
4 geodesic domes


goals table:

ID RESOURCE_ID GRADE SUBJECT
1 1 1 English
2 1 1 Soc
3 1 2 English
4 2 1 English
5 2 3 Soc
6 3 2 English
7 4 1 English

Now, how do I select all the resources which have 1st and 2nd grade
English goals? If I just do:

Select * from resources, goals where ((resources.ID =
goals.RESOURCE_ID) and (SUBJECT="English") and ((GRADE="1") and
(GRADE="2")));

I'll get no results, since no record of the joined set will have more
than one grade. I can't just put 'or' between the Grade
conditions; that would give resources 1, 2, 3, and 4, when only 1
really should match.

My real problem is slightly more complex, as the 'goals' table also
contains an additional field which might be searched on.

I'm thinking it's time for me to go into the deep end of SQL (MySQL,
actually), and my old O'Reilly MySQL & mSQL book isn't doing the
trick.

Surely this has come up before - thanks for any guidance.

- AM Thomas

 >> Stay informed about: select where multiple joined records match 
Back to top
Login to vote
db_rookie

External


Since: Feb 12, 2005
Posts: 2



(Msg. 2) Posted: Sun Feb 13, 2005 6:05 pm
Post subject: Re: select where multiple joined records match [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

You want a join, and it is better imho to make it explicit so you can
see more clearly what you're doing (that's the way I do it, anyway).
The join takes care of lining up the resources table and the goals
table on the resourcesID.

[By the way, I find it easier to name the tables in the singular, since
when you write code for them, you will be referring to them (and
thinking of them) in the singular. You will have a "goal" and a
"resource". Try it and you'll see.]

Try:

SELECT DISTINCT title FROM resources
LEFT JOIN goals on goals.resourcesID = resources.ID
WHERE grade IN (1,2) AND subject LIKE 'english'

You'll need the DISTINCT so you don't get repeated titles.

anmar.TakeThisOut@intrex.net wrote:
 > I'm trying to figure out how to select all the records in one table
 > which have multiple specified records in a second table.
 >
 > Here's a simplified version of my problem.
 >
 > I have two tables, resources and goals.
 >
 > resources table:
 >
 > ID TITLE
 > 1 civil war women
 > 2 bunnies on the plain
 > 3 North Carolina and WWII
 > 4 geodesic domes
 >
 >
 > goals table:
 >
 > ID RESOURCE_ID GRADE SUBJECT
 > 1 1 1 English
 > 2 1 1 Soc
 > 3 1 2 English
 > 4 2 1 English
 > 5 2 3 Soc
 > 6 3 2 English
 > 7 4 1 English
 >
 > Now, how do I select all the resources which have 1st and 2nd grade
 > English goals? If I just do:
 >
 > Select * from resources, goals where ((resources.ID =
 > goals.RESOURCE_ID) and (SUBJECT="English") and ((GRADE="1") and
 > (GRADE="2")));
 >
 > I'll get no results, since no record of the joined set will have more
 > than one grade. I can't just put 'or' between the Grade
 > conditions; that would give resources 1, 2, 3, and 4, when only 1
 > really should match.
 >
 > My real problem is slightly more complex, as the 'goals' table also
 > contains an additional field which might be searched on.
 >
 > I'm thinking it's time for me to go into the deep end of SQL (MySQL,
 > actually), and my old O'Reilly MySQL & mSQL book isn't doing the
 > trick.
 >
 > Surely this has come up before - thanks for any guidance.
 >
 > - AM Thomas<!-- ~MESSAGE_AFTER~ -->

 >> Stay informed about: select where multiple joined records match 
Back to top
Login to vote
Display posts from previous:   
   Database Help (Home) -> mySQL All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]