Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SELECT_Join problem... :)

Status
Not open for further replies.

Assassin

Programmer
Oct 5, 1999
1
EE
Lets say i hawe Tables A,B,C.<br>

A is the client table, B is the product table and C contains warrant replacments, it contains 2 id fileds from A and same for B, now i need to create select that joins it all togetger, how te heck i can do that?<br>

<br>

A)<br>

Name<br>

ID<br>

<br>

B) <br>

Name<br>

ID<br>

<br>

C)<br>

BadProduct (B.Name)<br>

Worker (A.Name)<br>

NewProduct (B.Name)<br>

ReplacedBy (A.Name)<br>

ID<br>

<br>

Select ????????????? FROM C ??????? JOIN ??????? WHERE C.ID=1<br>

<br>

<br>

************HELP!!!!! *********************
 
This is ANSI SQL (only kind I know) so I hope that's ok.<br>
<br>
Select a.field, b.field, c.field<br>
FROM A,B,C<br>
WHERE C.ID=1<br>
and c.id=a.id<br>
and c.id=b.id;<br>
<br>
Regards<br>
<br>
Mike<br>
<br>
<p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= > </a><br>
 
How about:<br>
<br>
SELECT replacement.id,<br>
old_product.name, <br>
old_worker.name, <br>
new_product.name,<br>
new_worker.name,<br>
FROM a old_worker,<br>
a new_worker,<br>
b old_product,<br>
b new_product,<br>
c replacement<br>
WHERE<br>
replacement.id = 1<br>
AND replacement.worker = old_worker.name<br>
AND replacement.bad_product = old_product.name<br>
AND replacement.replaced_by = new_worker.name<br>
AND replacement.newproduct = new_product.name;<br>
<br>
My only comment would be that it might for a better design to use the ID columns from tables A and B in Table C instead of the name columns.
 
I like Mike's best - its simple and should give you all the data you need<br>
<br>
C
 
Yes, Mike's solution is much simpler and will work if A, B, and C all share the same ID value. <br>
<br>
However, I believe the problem is that table C is really defined as (Assassin - Please confirm):<br>
<br>
BadProduct (B.ID%TYPE)<br>
Worker (A.ID%TYPE)<br>
NewProduct (B.ID%TYPE)<br>
ReplacedBy (A.ID%TYPE)<br>
ID<br>
<br>
If Worker &lt;&gt; ReplacedBy and/or BadProduct &lt;&gt; NewProduct within any given row, then I believe you would have to use my query (or something like it) in order to extract the names of all clients and products involved.<br>
<br>
For instance, if Table A contains rows<br>
<br>
ID Name<br>
1 Smith<br>
2 Jones<br>
<br>
<br>
and Table B contains<br>
<br>
ID Name<br>
1 Metal Rotor<br>
2 Plastic Rotor<br>
3 Carbon Rotor<br>
<br>
<br>
and Table C contains<br>
<br>
ID BadProduct Worker NewProduct ReplacedBy<br>
1 1 1 2 2<br>
<br>
I believe Mike's query would look something like (Mike - is this what you meant or did I misread you?) :<br>
<br>
SQL&gt; select a.name &quot;1st Worker&quot;, <br>
b.name &quot;1st Part&quot;, <br>
a.name &quot;2nd Worker&quot;, <br>
b.name &quot;2nd Part&quot;<br>
from a,b,c<br>
where c.id = 1<br>
and c.id = a.id<br>
and c.id = b.id;<br>
<br>
1st Worker 1st Part 2nd Worker 2nd Part<br>
-------------------- -------------------- -------------------- --------------------<br>
Smith Metal Rotor Smith Metal Rotor<br>
<br>
which would not be a correct response here.<br>
<br>
That is why my query includes multiple references to the same table. In this case, my query/response looks like:<br>
<br>
SELECT replacement.id,<br>
old_product.name &quot;1st Part&quot;,<br>
old_worker.name &quot;1st Worker&quot;,<br>
new_product.name &quot;2nd Part&quot;,<br>
new_worker.name &quot;2nd Worker&quot;<br>
FROM a old_worker,<br>
a new_worker,<br>
b old_product,<br>
b new_product,<br>
c replacement<br>
WHERE<br>
replacement.id = 1<br>
AND replacement.worker = old_worker.id<br>
AND replacement.badproduct = old_product.id<br>
AND replacement.replacedby = new_worker.id<br>
AND replacement.newproduct = new_product.id<br>
<br>
ID 1st Part 1st Worker 2nd Part 2nd Worker<br>
--------- -------------------- -------------------- -------------------- --------------------<br>
1 Metal Rotor Smith Plastic Rotor Jones<br>
<br>
<br>
Assassin - do either of these solve your problem?<br>
(Sorry about the formatting, but blank spaces keep getting eliminated and TAB doesn't work here!)
 
You lost me there carp - sorry<br>
<br>
Actually - Assasin hasn't logged in since (s)he posted the question. &lt;smile&gt; Shame.<br>
<br>
Maybe our comments will assist someone else.<br>
<br>
-ml<br>
<br>
<p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= > </a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top