By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,655 Members | 1,875 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,655 IT Pros & Developers. It's quick & easy.

Joining multiple fields to a single table?

P: n/a
Hi I am having difficulty writing an SQL query to do what I want:

I have 1 table with 2 columns, 'id' and 'name':

tbl_names:
id name
-- ----
1 Bob
2 Jeff
3 Fred
4 Joe
5 Bill
I then have another table which contains several fields which hold id's
from the above table:

tbl_output:
id person1 person2 person3
-- ------- ------- -------
1 2 3 1
2 5 4 3
I need a query that will return the names for the specified id from
tbl_output.
If I have just one 'person' field in tbl_output I would do it with an
inner join like this:

SELECT name from tbl_names
INNER JOIN tbl_names on tbl_names.id = tbl_output.person
WHERE tbl_output.id = ?

but I can't figure it out when theres multiple fields to be joined from
the same table...e.g I want to specify tbl_output.id = 1, and it give me:

person1 person2 person3
------- ------- -------
Jeff Fred Bob
Any ideas?

Cheers,
Ben.
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Ben Willcox wrote:
but I can't figure it out when theres multiple fields to be joined from
the same table...e.g I want to specify tbl_output.id = 1, and it give me:

person1 person2 person3
------- ------- -------
Jeff Fred Bob


SELECT n1.name as person1, n2.name as person2, n3.name as person3
from tbl_names t inner join tbl_output p on t.id = p.id
inner join tbl_names n1 on p.person1 = n1.id
inner join tbl_names n2 on p.person2 = n2.id
inner join tbl_names n3 on p.person3 = n3.id
where t.id = ?

Supply the value "1" for the "?" parameter.

Regards,
Bill K.
Jul 20 '05 #2

P: n/a
Bill Karwin wrote:
but I can't figure it out when theres multiple fields to be joined
from the same table...e.g I want to specify tbl_output.id = 1, and it
give me:

person1 person2 person3
------- ------- -------
Jeff Fred Bob

SELECT n1.name as person1, n2.name as person2, n3.name as person3
from tbl_names t inner join tbl_output p on t.id = p.id
inner join tbl_names n1 on p.person1 = n1.id
inner join tbl_names n2 on p.person2 = n2.id
inner join tbl_names n3 on p.person3 = n3.id
where t.id = ?

Supply the value "1" for the "?" parameter.


Thanks Bill that worked perfectly in my test database. However when I
tried to modify it for the live app with a slightly different dataset,
it didn't work straight away. I belive this is because the records in my
tbl_names table start at an id of around 700, therefore the first JOIN
cannot link the two id fields together.
I'm still unsure why that first join is required, or how it works, as
there is no relationship between the two id fields? - is it just to
limit the output to 1 record?
Anyway, I managed to get around it by removing the first join, which
cause the query to return lots of identical records (with the data I
want), and adding LIMIT 1 to just give me one of them. Do you think this
is the best way to do it?

Thanks again for your help,

Ben.
Jul 20 '05 #3

P: n/a
Ben Willcox wrote:
I'm still unsure why that first join is required, or how it works, as
there is no relationship between the two id fields? - is it just to
limit the output to 1 record?
Ah, I was confused about the query you were trying to achieve. Yes,
you're right to take out the first join.
Anyway, I managed to get around it by removing the first join, which
cause the query to return lots of identical records (with the data I
want), and adding LIMIT 1 to just give me one of them. Do you think this
is the best way to do it?


That works, but you can also use "SELECT DISTINCT ..."

Regards,
Bill K.
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.