Wednesday, March 7, 2012

Help needed

Hi all

I m having three tables youth,clubs,ythclubs. youth may participate in any no of clubs.
in youth youth_id is the key attribute. In clubs club_id is the key attribute.Table ythclubs consists the columns youth_id,club_id. My requirement is
If user selects a club, it should give the list of youths in that club along with the some columns of youth(for ex lastname,firstname like this)

Sample data is
This is in Youths table This is in clubs table
Youthid club_id
10005 10003
10006 10004
10007 10005
10008 10000

In ythclub the data is
club_id youthid
10005 10005
10003 10005
10004 10005
10006 10006
10005 10012
10006 10012

Thank u

Baba

It sounds to me like you should be able to do this with a select that joins the Youth and ythclub tables:

Code Snippet

declare @.Youth table
( YouthId integer,
lastName varchar(12),
firstName varchar(12)
)
insert into @.Youth
select 10005, 'Rubble', 'Fred' union all
select 10006, 'Flintstone', 'George' union all
select 10007, 'Jetson', 'Barney' union all
select 10008, 'Douglas', 'Jed' union all
select 10012, 'Kiley', 'Oliver'
--select * from @.youth

declare @.ythClub table
( club_id integer,
youthId integer
)
insert into @.ythClub
select 10005, 10005 union all
select 10003, 10005 union all
select 10004, 10005 union all
select 10006, 10006 union all
select 10005, 10012 union all
select 10006, 10012
--select * from @.ythClub

declare @.targetClub integer
set @.targetClub = 10005

select a.club_id,
a.youthId,
b.lastName,
b.firstName
from @.ythClub a
join @.Youth b
on a.youthId = b.youthId
and a.club_id = @.targetClub
order by b.lastName, b.firstName

/*
club_id youthId lastName firstName
-- --
10005 10012 Kiley Oliver
10005 10005 Rubble Fred
*/

If you need anything from the CLUB table -- such as the name of the club -- then you will also need to join to the CLUB table.

No comments:

Post a Comment