Jump to content

[SOLVED] Alternative to JOIN


JSHINER

Recommended Posts

I have the following tables:

 

Table A

ID | NAME | A | B | C | D | E | F

1  |  John | 1 | 5  | 3 | 3 | 5 | 6

 

 

Table B

 

ID | NAME

1  | Name 1

2  | Name 2

3  | Name 3

4  | Name 4

5  | Name 5

6  | Name 6

 

---

 

Now correct me if I'm wrong, but I can't do a JOIN where table_a.a = table_b.id, table_a.b = table_b.id, right?

 

Is there any way I can do this?

Link to comment
Share on other sites

Desired output would be: John - Name 1 - Name 5 - Name 3, etc...

Shouldn't be a problem... you just need to join table b as many times as you have columns for A-F (just be sure to use a different alias for each)...

 

e.g. select a.name, ba.name, bb.name, bc.name

from tableA as a

inner join tableB as ba on ( ba.id = a.a )

inner join tableB as bb on ( bb.id = a.b )

inner join tableB as bc on ( bc.id = a.c )

 

etc.

Link to comment
Share on other sites

Am I creating a.name,  ba.name, etc.

 

Sorry I had two ID and NAME columns - confusing. Let's try again with:

 

Table A

ID | NAME | A | B | C | D | E | F

-------------------------------------

1  |  John | 1 | 5  | 3 | 3 | 5 | 6

 

 

Table B

 

ID_CAT | Category

---------------------

1        | Apple

2        | Banana

3        | Orange

4        | Plum

5        | Cookie

6        | Coffee

 

With output of: John - Apple, Cookie, Orange, Orange, Cookie, Coffee

 

Sorry, first time with inner joins  :)

Link to comment
Share on other sites

select concat( a.name, ' - ', concat_ws( ', ', ba.Category, bb.Category, bc.Category )
from tableA as a
inner join tableB as ba on ( ba.id_cat = a.A )
inner join tableB as bb on ( bb.id_cat = a.B )
inner join tableB as bc on ( bc.id_cat = a.C )

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.