Pergunta

Hello fellow travelers of Stack Overflow. I'm not entirely certain how to ask this other than with pictures to better explain what I am getting at. If by some chance this has been asked elsewhere and I failed at finding it I apologize.

I have 2 tables tableA and tableB. TableA containes a column marked id and displayName of which there are several dozen items ( for simplicity, we'll assume only 5 ). The ID column is unique and auto incremented it also is the primary key for searching. The second table (TableB) contains an id as well for that item, but also 4 references to TableA's id's and a few extraneous pieces of information that are irrelevant. So it appears like this:

TableA
-------------------------
|  id    |  displayName  |
-------------------------
|  1     |  itemA        |
|  2     |  itemB        |
|  3     |  itemC        |
|  4     |  itemD        |
|  5     |  itemE        |
-------------------------

TableB
--------------------------------------------------------
|  id    | name |  varA   |  varB   |  varC   |  varD   |
--------------------------------------------------------
|  1     | elm1 |    3    |    4    |    2    |    5    |
|  2     | elm2 |    4    |    5    |    2    |    1    |
|  3     | elm3 |    1    |    4    |    3    |    2    |
|  4     | elm4 |    2    |    1    |    5    |    4    |
|  5     | elm5 |    3    |    4    |    5    |    1    |
--------------------------------------------------------

I currently have a query that will collapse the values and give me the information I require: SQL: SELECT tB.name, tB.varA, tB.varB, tB.varC, tB.varD FROM tableB tB GROUP BY tB.name

Now what I would LIKE to do is instead of having varA, varB, etc show up as numbers, is instead have the data correlated with TableA and the displayName inserted. So instead of

Result
-----------------------------------------------
| name |  varA   |  varB   |  varC   |  varD   |
-----------------------------------------------
| elm1 |    3    |    4    |    2    |    5    |
| elm2 |    4    |    5    |    2    |    1    |
-----------------------------------------------

It would show:

Result
-----------------------------------------------
| name |  varA   |  varB   |  varC   |  varD   |
-----------------------------------------------
| elm1 |  itemC  |  itemD  |  itemB  |  itemE  |
| elm2 |  itemD  |  itemE  |  itemB  |  itemA  |
-----------------------------------------------
Foi útil?

Solução

You will just need to do 4 joins after the grouping.

P.S. I do not understand, why you group the rusults by name in tableB.

Assuming you do not have to make groupung that way (in other case the other values do not make much sense):

SELECT b.name,
    aa.displayName as varA,
    ab.displayName as varB,
    ac.displayName as varC,
    ad.displayName as varD
FROM tableB as b
JOIN tableA as aa ON b.varA = aa.id
JOIN tableA as ab ON b.varB = ab.id
JOIN tableA as ac ON b.varC = ac.id
JOIN tableA as ad ON b.varD = ad.id;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top