简体   繁体   中英

mySQL: Merging columns from the same table keeping values in the same row

I need to arrange the values from one table with some strange conditions.
My table is:
| ref | value1 | info1 | value2 | info2 | value3 | info3 |
| 9 | 100 | test1 | 220 | test4 | 300 | test7 |
| 3 | 150 | test2 | 250 | test5 | 400 | test8 |
| 7 | 200 | test3 | 290 | test6 | 500 | test9 |

I need to be able to get a result such as:
| ref | values | infos |
| 9 | 100 | test1 |
| 3 | 150 | test2 |
| 7 | 200 | test3 |
| 9 | 220 | test4 |
| 3 | 250 | test5 |
| 7 | 290 | test6 |
| 9 | 300 | test7 |
| 3 | 400 | test8 |
| 7 | 500 | test9 |

I can't even start to understand if what I need is possible and I can't get my head around it. Any help will be most appreciated.

You could use UNION ALL

SELECT ref, value1 as values, info1 as Infos FROM tbl
UNION ALL
SELECT ref, value2 as values, info2 as Infos FROM tbl
UNION ALL
SELECT ref, value3 as values, info3 as Infos FROM tbl

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM