mysql merging columns
Say i have the following MySql 'orders' table:
id|car|car_qty|speakers|speakers_qty
1 | 2 | 15 | 5 | 16
2 | 2 | 19 | 5 | 40
What is the query to get the following:
product|qty
2 | 15
2 | 19
5 开发者_运维百科 | 16
5 | 40
WITHOUT using UNION?
Thank you!
Try this:
SELECT car product,car_qty qty FROM orders
UNION
SELECT speakers product,speakers_qty qty FROM orders
Do a union ALL from the same table... Just need to make sure the column names and data types are the same...
select
car as product,
car_qty as qty
from
orders
union all
select
speakers as product,
speakers_qty as qty
from
orders;
精彩评论