Jump to content

3 deep joins


mbeals

Recommended Posts

I have 4 tables that I need to join together in a not so simple way and my brain is hurting.

 

Table one is a table of addresses..nothing fancy.  Table two contains a list of customers, where one of the attributes is a reference back to an index in the address table.  Table three is a table of cable modems which has an attribute which is an index in the customer table.

 

So joining all three tables on these ID's produces a list of customer name, address, and modem info.  I have that part of the query working and it's structured like this:

 

Select customers.*, modems.*, addresses.*, Walkdata.* FROM addresses
join customers on customers.addID = addresses.index
join modems on modems.custID = customers.index

 

This produces name, address, MAC address (and some other stuff) for all modems that are registered as subscribers.

 

Now, I have another table, which is the list of modems (and stats) for all modems that are connected to the cable plant.  I need to take this info and match it against the table we just created based on mac address (a common attribute), but I don't want to throw out non matching rows from either table (so I can see who isn't connected as well as unregistered modems attempting to connect).

 

I'm sure the answer is stupidly simple, but my brain is shutting down.

Link to comment
Share on other sites

that will truncate the non matching rows from the right table though.  I need to preserve those rows.

 

Say I have these two tables:

 

subscribers

Name      MAC

John1

Jane2

 

 

active modems

 

MAC    status

1online

3online

 

I need to create:

 

Name  MAC  status

John1online

Jane2[/td]

3online

 

the difference is that the first table is the result of two joins

Link to comment
Share on other sites

doing some googling, I think I need a full outer join, but mysql doesn't support that yet.

 

Any thoughts on a workaround?  I saw one where you perform a union to a right join, but with the additional joins, that seems really clunky.  Can I turn the first set of joins into a sub query and just perform the two sets of joins on that?

Link to comment
Share on other sites

well I think i figured out a workaround, but it's complicated.

 

Select
customers.name, customers.phone,
modems.mac, modems.qos, modems.subdate,
addresses.num, addresses.street, addresses.unit, addresses.building, 
Walkdata.MAC, Walkdata.SNR, Walkdata.RX, Walkdata.STATUS
FROM addresses
join customers on customers.addID = addresses.index
join modems on modems.custID = customers.index
left join Walkdata on Walkdata.MAC = modems.mac
union
Select '','','','','','','','','',`MAC`, `SNR`, `RX`, `STATUS` 
FROM Walkdata 
where  `MAC` NOT IN (select `mac` from modems)"

 

It does work, but can it be optimized?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.