Like UNPIVOT in SQL!
Question
How to change the way that cols orders it self?
If we have a table like this
a b c
1 xx xy
2 xy yx
3 yy xx
Since both of b and c are same type I would like to aggregate them in a one col call E respectively
a E
1 xx
1 xy
2 xy
2 yx
3 yy
3 xx
Is that possible using MYSQL 5.7 ?
Solution
You can just do it with a UNION ALL
(you may or may not need the ORDER BY
part, depending on your use-case):
SELECT
a, b as `E`
FROM
a
UNION ALL
SELECT
a, c as `E`
FROM
a
ORDER BY
a ;
You can check it here.
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange