473,396 Members | 1,766 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.

Join with intermediate table

Hi,

I have 3 tables: tPart, tMachine and a table linking these 2:
tMachinePart

tPart: ID, ... partcolumns
tMachinePart: machineid, partid
tMachine: ID,... machinecolumns

I would like to make a join like this:
SELECT tPart.*, tMachine.*
FROM tPart INNER JOIN (tMachine INNER JOIN tMachinePart ON tMachine.ID
= tMachinePart.machineID) ON tPart.ID = tMachinePart.partID;

But the result should be that
- only one part is shown, even with multiple machines
- if no machine is linked to a part, the part should be shown, and
machinecolumns should be NULL

Kind regards,

Thomas

Nov 13 '05 #1
2 1440

modialsi wrote:
Hi,

I have 3 tables: tPart, tMachine and a table linking these 2:
tMachinePart

tPart: ID, ... partcolumns
tMachinePart: machineid, partid
tMachine: ID,... machinecolumns

I would like to make a join like this:
SELECT tPart.*, tMachine.*
FROM tPart INNER JOIN (tMachine INNER JOIN tMachinePart ON tMachine.ID
= tMachinePart.machineID) ON tPart.ID = tMachinePart.partID;

But the result should be that
- only one part is shown, even with multiple machines
- if no machine is linked to a part, the part should be shown, and
machinecolumns should be NULL


If you use an INNER JOIN and one of the columns used in the JOIN is
NULL, then that row will not appear in the results. Try using an OUTER
JOIN instead.

Edward

Nov 13 '05 #2
Thank you for your reply.

It is indeed correct that I should use a outer join, something like
this:

SELECT tPart.ID AS partid, tPart.TypeID AS parttypeid, tPart.PN,
tPart.DealerPrice, tPart.Versiondate, tPart.TranslatorID,
tPart.nimupdate, tPart.sellmultiple, Max(tRevision.revisionNumber) AS
partrevisionnumber, tPart.Schems, Count(tMachinePart.machineID) AS
machineqty, tMachine.model, tMachine.typeid AS machinetypeid,
tMachine.serialnumber, tMachine.id AS machineid
FROM [SELECT top 1 tMachine.id,model,typeid,serialnumber FROM
tMachine]. AS tMachine RIGHT JOIN ((tRevision RIGHT JOIN tPart ON
tRevision.partID=tPart.ID) LEFT JOIN tMachinePart ON
tPart.ID=tMachinePart.partID) ON tMachine.ID=tMachinePart.machineID
GROUP BY tPart.ID, tPart.TypeID, tPart.PN, tPart.DealerPrice,
tPart.Versiondate, tPart.TranslatorID, tPart.nimupdate,
tPart.sellmultiple, tPart.Schems, tMachine.model, tMachine.typeid,
tMachine.serialnumber, tMachine.id;
But the second problem remains: how do I get all the parts, but only
one machine for a part.

The reason the query above doesn't work is that I get only the first
machine in the tMachine table, not the correct machine corresponding to
the part.

Any idea on how to solve this?

Thomas

Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: B. Fongo | last post by:
I learned MySQL last year without putting it into action; that is why I face trouble in formulating my queries. Were it a test, then you would have passed it, because your queries did help me...
4
by: Omavlana | last post by:
Hi, I need your help to resolve this problem. I have written a right outer join query between 2 indipendent tables as follows. select b.Account_desc, b.Account, a.CSPL_CSPL from...
13
by: kieran | last post by:
Hi, I have the following SQL statement which is pulling a few details from a database. As you can see, there is only the one table from which i am creating a temporary copy. The reason I do...
7
by: stabbert | last post by:
I am attempting to join two tables together on two different unix servers. Here is some relevant info about the tables. TABLE 1 Setup ----------------------- DB2 UDB 7.2 EE
5
by: Prakash | last post by:
Hi, In my Access Report ... If there is "NO" entry i.e. (no corresponding CODE) in my Data-File, then even the Master-File entry does not show up in the report. eg. Mast-File Code Op.Bal
5
by: Lyn | last post by:
This one is difficult to explain, so I will cut it down to the basics. I have a major table 'tblA' which has an autonum field 'ID-A' as primary key (of no significance to users). 'tblA' contains...
3
by: CanFlightSim | last post by:
I am not very strong in databases, so you must forgive any obvious stupidity. After having a good look at our Sales Managemend database, we decided there is something basially wrong. Our Agents...
2
by: Veeru71 | last post by:
Hi, 1) Is it possible to force the join order of the tables in a query (similar to "hints" in Sybase, etc) ? For Example, I am joining 3 tables - A,B & C in my SELECT stmt, and I would like...
23
by: Summercool | last post by:
i think in Ruby, if you have an array (or list) of integers foo = you can use foo.join(",") to join them into a string "1,2,3" in Python... is the method to use ",".join() ? but then it...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.