473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Joining multiple fields to a single table?

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
3 8777
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: James | last post by:
Can anyone please shed some light on the following... I have a framework that uses dynamically created tables, named using an incremental "attribute set ID", as follows: attrdata_1 attrdata_2...
6
by: Christopher Harrison | last post by:
Is there a way to store an indefinite number of keys in one field and self join them? Imagine, for example, you have a table of users with a "friends" column. Say user 1 is friends with users 9, 7,...
11
by: dskillingstad | last post by:
I've been struggling with this problem for some time and have tried multiple solutions with no luck. Let me start with, I'm a novice at Access and I'm not looking for someones help to design my...
0
by: rayone | last post by:
Hi folks. I need advice. 2 options, which do you think is the better option to display/retrieve/report on the data. Keep in mind reporting (Crystal), SQL Performance, VB Code, usability,...
1
by: Steve C | last post by:
Hi, I'm having problems constructing a nested join. It's quite complex, so here's a simplfied example of the problem. Any thoughts on what I'm doig wrong - or if I've got the whole approach...
12
by: veaux | last post by:
Question about joins in queries. I have 2 tables with a field called "ID". Table 1 Rec1 = Jan12FredFlintstone Rec2 = Feb01WilmaRubble Table 2 Rec1 = Jan12BarneyRubble Rec2 = Mar03SamSlate
1
by: Shizbart | last post by:
MS Access 97 Beginner/Moderate Level User I am trying to create a Database to track Workouts in MS Access 97. I have one Table named Workouts that contains the following Fields: Workout...
13
by: paquer | last post by:
Ok' I have 2 tables. The first being my main table where each record has a 'FigureID' txt box. (Indexed - No duplicates) The second being my sub table where reporting is entered for these...
18
by: Apple001 | last post by:
Hi all! I am having trouble with joining multiple rows into one row. I will appreciate any help. For columns in the query, I have: invID(autot number), entryDate, invDate, vendor, invoiceAmount...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.