开发者

mysql merging columns

开发者 https://www.devze.com 2023-02-24 16:43 出处:网络
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

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;
0

精彩评论

暂无评论...
验证码 换一张
取 消