Jump to content

joining 2 tables more than once


chriso20

Recommended Posts

Hey,

 

I have 2 tables - 'members' and 'types'.

 

Each member can have 3 'types'.

 

So the 'member' table has 4 columns; name, typeA, typeB & typeC.

 

and the 'type' table has 2 columns; id & type.

 

At the moment, to find the first of a members type i use the following SQL:

 

SELECT m.name, t.type

FROM members m

INNER JOIN type t

ON m.typeA = t.id

 

how can i find the other 2 types (typeB and typeC) for each member?

 

Thanks for any help!

 

p.s. I use a separate table because there's a LOT of members and a lot of text for the 'types'.

Link to comment
Share on other sites

This is not a normalised layout. Normalise it first, otherwise you'll have to do the following:

SELECT m.name
      ,t1.type as 'typeA'
      ,t2.type as 'typeB'
      ,t3.type as 'typeC'
FROM members m
JOIN type t1 ON m.typeA = t1.id
JOIN type t2 ON m.typeB = t2.id
JOIN type t3 ON m.typeC = t3.id

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.