0

I need to 'convert' my table headers to columns and 'join' the attributing value. This is more a formatting issue than say a pivot table issue as I do not need to add up or combine multiple values and i will only ever have 1 result returned.

I run a query to get the particular record i need.

SELECT * FROM member WHERE id = 1;

RESULT:

id | name | height | hair colour | eye colour
---------------------------------------------
1  | john | 6ft    | black       | blue

The result that i would like to be able to have straight away out of the query is:

Feature     | Description
-------------------------
name        | john
height      | 6ft
hair colour | black
eye colour  | blue

So I could be wrong in saying its 'just' a formatting issue but reading through various posts on Pivot tables etc, has made me more confused over how to go about something like this.

Any solutions to this would be much appreciated.

Thanks in advance guys.

Answer:

After unconfusing myself with pivoting - i just did mini select statements for each column and value and UNION them together.

select 'Feature', 'Description'
union all
select 'Name:' , name from member where id = 1
union all
select 'Height:', height from member where id = 1
union all
select 'Hair Colour:', hair_colour from member where id = 1
union all
select 'Eye Colour:', eye_colour from member where id = 1
  • This is called unpivoting. The linked duplicate topic describes the static unpivoting (fixed list of union all statements), but also describes how to do it dynamically. – Shadow Jun 08 '17 at 16:59
  • ah ok ... I guess I was confusing myself by looking at pivoting rather than unpivoting. I will have a look through your example there. Thanks for this. – ConorBeckett Jun 08 '17 at 17:04
  • Pivoting is transposing rows to columns - at least in the database products. – Shadow Jun 08 '17 at 17:07
  • Sorted thanks. Was just confusing myself every which way for some reason even though i do remember using the union stuff was back in school. ill update an answer and close the question. cheers again – ConorBeckett Jun 08 '17 at 17:55

0 Answers0