Jump to content

Joining 3 tables and searching 2nd tables multiple rows


lucius

Recommended Posts

I have three tables:

table1 (id, name, etc), table2 (id, title, etc), table3 (table1.id, table2.id)

 

For example:

I want to pull up row in table1 (mechanisms) individually and link it to all of its relatives in table2 (parts) which is linked by table3 (which mechanisms have specific parts).

 

A row in table1 can be linked to any number of rows in table2.

 

So I want to find out what types of parts a car has, this is what I'd want to see:

------------+--------+-----------+--------+-----------+--------+-----------+---------+

| table1.id |  name  | table2.id | title  | table2.id | title  | table2.id | title  |

------------+--------+-----------+--------+-----------+--------+-----------+---------+

| 1        | car    | 3        | brakes | 8        | tires  | 5        | engine  |

------------+--------+-----------+--------+-----------+--------+-----------+---------+

 

How do I go about writing a query for this?

 

I really appreciate the help; I've been beat by this one!

Link to comment
Share on other sites

Hi

 

In simple terms you can't do it.You would get a variable number of columns for each row.

 

You could either do:-

 

SELECT table1.id, table1.name, table2.id, table2.title

FROM table1

LEFT OUTER JOIN table3 ON table1.id = table3.table1id

LEFT OUTER JOIN table2 ON table3.table2id = table2.id

 

That would give you one row for every valid combination of tables 1 and 2.

 

You could concatenate the results up using GROUP_CONCAT, but this would result in the details being difficult to process if you needed the individual mechanisms.

 

SELECT table1.id, table1.name, GROUP_CONCAT(table2.title)

FROM table1

INNER JOIN table3 ON table1.id = table3.table1id

INNER JOIN table2 ON table3.table2id = table2.id

GROUP BY table1.id, table1.name

 

All the best

 

Keith

Link to comment
Share on other sites

Actually, I did get it solved.  It was very simple:

 

SELECT *

FROM mechanisms as m, linked as l1, linked as l2, linked as l3

WHERE m.id = l1.mid

AND m.id = l2.mid

AND m.id = l3.mid

AND l1.partsid = 1

AND l2.partsid = 4

AND l3.partsid = 9

 

Of course I don't hand code this, rather I use a PHP foreach loop.  It's an easy way to manage many-to-many relationships.

Link to comment
Share on other sites

Hi

 

Trouble with that is that you need to know the number of final columns to build the piece of SQL in the first place. As that would mean a couple of seperate SQL calls it might well be more efficient to just use the group concat. Depends on what you want to do with the data once you have got it.

 

All the best

 

Keith

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.