0

I have a table like this:

id  -  name  -  value
1   -  shoes - 21
1   -  gloves - 5
2   -  shoes - 23
2   -  gloves - 3

I want it to be converted to a table like this:

id  -  shoes  - gloves
1   -  21     - 5
2   -  23     - 3

Is it possible to do it with one query in Mysql? I also want the name of columns to be generated from the name column in the 1st table.

1 Answers1

0

You may try to use join like this:

SELECT main.id, s.name AS shoes, g.name AS gloves
FROM tbl AS main
LEFT JOIN tbl s ON main.id = s.id
LEFT JOIN tbl g ON main.id = g.id
cn007b
  • 16,596
  • 7
  • 59
  • 74