Nowa kolumna w tabeli po zastosowaniu join

0

Mam tabelę A i B.

db

W wyniku złączenia powstaje mniej więcej coś takiego:

A.id | A.col | B.name | B.value
---------------- | -------------------
1 | kolumna1 | atrybut1 | 1
1 | kolumna1 | atrybut2 | 10
1 | kolumna1 | atrybut3 | 100
2 | kolumna2 | atrybut1 | 2
2 | kolumna2 | atrybut2 | 20
2 | kolumna2 | atrybut3 | 200
3 | kolumna3 | atrybut1 | 3
3 | kolumna3 | atrybut3 | 300

Każdy atrybut chcę wyświetlić w osobnej kolumnie.
Udało mi się powyższy wynik zamienić na:

A.id | A.col | attr1 | attr2 | attr3
---------------- | -------------------
1 | kolumna1 | 1 | null | null
2 | kolumna2 | 2 | null | null
3 | kolumna3 | 3 | null | null
1 | kolumna1 | null | 10 | null
2 | kolumna2 | null | 20 | null
1 | kolumna1 | null | null | 100
2 | kolumna2 | null | null | 200
3 | kolumna3 | null | null | 300

Jednak nie wiem w jaki sposób usunąć nadmiarowe nulle.
Ostateczny rezultat jaki próbuję osiągnąć:

A.id | A.col | attr1 | attr2 | attr3
---------------- | -------------------
1 | kolumna1 | 1 | 10 | 100
2 | kolumna2 | 2 | 20 | 200
3 | kolumna3 | 3 | null | 300

I właśnie z tym ostatnim jest problem - co powinno zawierać w sobie takie zapytanie?

1

PIVOT, TRANSFORM

0

W mysql nie ma czegoś takiego jak PIVOT.

Próbuje się posiłkować tym https://stackoverflow.com/questions/15462753/mysql-join-multiple-rows-as-columns

Stworzyłem zapytanie:

select A.id, A.col,
  MAX(CASE WHEN B.p_id = 1 THEN B.value ELSE NULL END) attr1,
  MAX(CASE WHEN B.p_id = 2 THEN B.value ELSE NULL END) attr2,
  MAX(CASE WHEN B.p_id = 3 THEN B.value ELSE NULL END) attr3
  from A inner join B 
  on A.p_id = B.p_id
  group by A.id;

Ale w wyniku dostaję ciągle nulle:

id | col | attr1 | attr2 | attr3
---------------- | -------------------
1 | kolumna1 | 100 | null | null
2 | kolumna2 | null | 200 | null
3 | kolumna3 | null | null | 300

1 użytkowników online, w tym zalogowanych: 0, gości: 1