Home > Software engineering >  Show each value in different rows
Show each value in different rows

Time:03-30

How can i show values from different columns in new rows? Lets say i have:

o_id type color
1 product red
2 object green

And i want my result to look like this:

o_id Attribute
1 product
1 red
2 object
2 green

CodePudding user response:

One simple approach uses a union:

SELECT o_id, type AS Attribute, 1 AS pos FROM yourTable
UNION ALL
SELECT o_id, color, 2 FROM yourTable
ORDER BY o_id, pos;
  • Related