mysql合并列

时间:2011-04-13 11:37:06

标签: mysql

说我有以下MySql'orders'表:

id|car|car_qty|speakers|speakers_qty  
1 | 2 |  15   |   5    |   16
2 | 2 |  19   |   5    |   40

获得以下内容的查询是什么:

product|qty
  2    | 15
  2    | 19
  5    | 16
  5    | 40

没有使用UNION?

谢谢!

2 个答案:

答案 0 :(得分:2)

试试这个:

SELECT car product,car_qty qty FROM orders
UNION 
SELECT speakers product,speakers_qty qty FROM orders

答案 1 :(得分:1)

从同一个表中执行union ALL ...只需要确保列名和数据类型相同...

select 
      car as product,
      car_qty as qty
  from
      orders
union all
select
      speakers as product,
      speakers_qty as qty
   from
      orders;