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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Joing Tabels Queryf

Status
Not open for further replies.

dg3249

MIS
Jun 11, 2002
41
US
I have three tables that I need to join and I am having some trouble with it. Please help..

Tables:

Client Orders NA_Orders
OFFICE_ID OFFICE_ID OFFICE_ID
group_id Order_id Order_id
group_id

Now, these three tables have the same field (office_id) but they can all have a different value. How can a write a join query to show all values from these three tables
Diana
 
You can only JOIN tables if there is a column with matching data in all of the tables. The general structure of the JOIN is explained in SQL BOL.



Example:
Select
c.OFFICE_ID,
c.Group_id,
o_Order_id
From Client c
Inner Join Orders o
On c.Office_ID = o_Office_ID
And c.Group_ID = o.Group_ID
Terry L. Broadbent - DBA
Computing Links:
faq183-874 contains "Suggestions for Getting Quick and Appropriate Answers" to your questions.
 
It appears as though Client and Orders could join on Group_ID, then Orders and NA_Orders could join on Order_ID, giving you something like ....

Code:
SELECT c.Office_ID, c.Group_ID, 
  o.Office_ID, o.Order_ID, 
  n.Office_ID
FROM Clients c
  JOIN Orders o ON c.Group_ID = o.Group_ID
    JOIN NA_Orders n ON o.Order_ID = n.Order_ID

Is this what you're looking for? Good luck! --Angel
-----------------------------------
SELECT * FROM users WHERE clue > 0
0 row(s) affected
 
I understand what you both have suggested, but its not exactly what I need. orders.office_id and na_orders.office_id may not necessarily have the same office_id for the same order, but one of those two fields will match with clients.office_id. So essentially what I need is to show all clients and their office_id's as well if they have any orders and their office_id is different than the clients.office_id then show it. I know its a little bit of a weird one.

Diana
 
Possibly a UNION?

Code:
SELECT c.Group_ID, c.Office_ID, o.Order_ID
FROM Clients c
  JOIN Orders o ON c.Office_ID = o.Office_ID
UNION
SELECT c.Group_ID, c.Office_ID, n.Order_ID
FROM Clients c
  JOIN NA_Orders n ON c.Office_ID = n.Office_ID
--Angel
-----------------------------------
SELECT * FROM users WHERE clue > 0
0 row(s) affected
 
Just browsing through and couldn't help but notice your tag line Angel!!

Leslie
landrews@metrocourt.state.nm.us

SELECT * FROM USERS WHERE CLUE > 0
No Rows Returned
 
I noticed yours also.

Great minds think alike. :) --Angel
-----------------------------------
Every time I lose my mind, I wonder
if it's really worth finding.
 
Rudy - I own the t-shirt!!!

Have you seen:

There are 10 types of people in the world -
those who understand binary and those who don't!!


Leslie
landrews@metrocourt.state.nm.us

SELECT * FROM USERS WHERE CLUE > 0
No Rows Returned
 
Hi Angel,

I have created a Union query and it seems to pull up the data correctly.
Now, I have other fields that I need to include as part of the query, how would you go about this without including it in the union itself.

D
 
If you need to include other fields in the result set, the additional columns will need to be added to each SELECT statement. If you have different inclusion criteria for Orders versus NA_Orders, modify the WHERE clause in the appropriate SELECT statement. You can even join to other tables, if necessary.

The key to UNION is to have identically structured result sets from your multiple SELECTs. The UNION will append one set to another and return a single result set. Note that UNION is distinct across the individual SELECTs. If the exact same result row is returned from two different SELECTs, only one instance will appear with the UNION. Use UNION ALL to see all instances. --Angel
-----------------------------------
Every time I lose my mind, I wonder
if it's really worth finding.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top