使用列将列值拆分为多个列

时间:2014-11-24 08:59:28

标签: mysql

使用SQL查询,我的当前表如示例A ,如何获取示例B 中的表:

示例A

+----------+------------+
| quantity | product    |
+----------+------------+
| 3        | apple      |
| 1        | orange     |
| 4        | kiwi       |
| 2        | banana     |
+----------+------------+

例B

+----------+------------+
| quantity | product    |
+----------+------------+
| 1        | apple      |
| 1        | apple      |
| 1        | apple      |
| 1        | orange     |
| 1        | kiwi       |
| 1        | kiwi       |
| 1        | kiwi       |
| 1        | kiwi       |
| 1        | banana     |
| 1        | banana     |
+----------+------------+

3 个答案:

答案 0 :(得分:1)

您可以使用大表生成数字并加入生成的数字序列。 'AnyBigTable'可以是一个表或查询,它至少返回记录数量作为您拥有的最大产品数量。因此,对于您的示例数据,它只需要4条记录来支持奇异果。

select
  1 as quantity,
  product
from
  YourTable t
  inner join 
    (select 
      @rownum := @rownum + 1 as num
    from
      AnyBigTable x
      , (select @rownum := 0) r
    ) n /* n.num contains numbers from 1 to rowcount of x */
  on /* Join the number table to get that amount of rows for the product */
    n.num <= t.quantity 
order by product

请参阅小提琴:http://sqlfiddle.com/#!2/fd210e/3

答案 1 :(得分:0)

另一种方法是为此创建一个程序。

delimiter //
create procedure split_tableA()
begin
 declare a_quantity int;
 declare a_product varchar(100);
 declare x  int;
 declare done int;
 declare cur cursor for select quantity,product from tableA ;
 declare continue handler for not found set done=1;
 set done = 0;
 open cur;
 data_loop: loop
   fetch cur into a_quantity,a_product;
   if done = 1 
    then leave data_loop; 
   end if;
   SET x = 1;
   while x<=a_quantity do
     insert into tableB (quantity,product) values (1,a_product);
     SET  x = x + 1; 
   end while;
 end loop data_loop;
 close cur;
end ;//

delimiter ;

以下是一些测试

mysql> select * from tableA;
+----------+---------+
| quantity | product |
+----------+---------+
|        3 | apple   |
|        1 | orange  |
|        4 | kiwi    |
|        2 | banana  |
+----------+---------+

mysql> select * from tableB;
Empty set (0.00 sec)

mysql> call split_tableA();
Query OK, 0 rows affected, 1 warning (0.32 sec)

mysql> select * from tableB;
+----------+---------+
| quantity | product |
+----------+---------+
|        1 | apple   |
|        1 | apple   |
|        1 | apple   |
|        1 | orange  |
|        1 | kiwi    |
|        1 | kiwi    |
|        1 | kiwi    |
|        1 | kiwi    |
|        1 | banana  |
|        1 | banana  |
+----------+---------+
10 rows in set (0.00 sec)

答案 2 :(得分:0)

避免变量和函数,将表与其他表交叉连接以生成一系列行(其中范围大于数量的最大值)。

例如

SELECT 1, current_table.product
FROM current_table
CROSS JOIN (SELECT 0 AS aCnt UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) units
CROSS JOIN (SELECT 0 AS aCnt UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) tens
WHERE current_table.quantity > (units.aCnt + 10 * tens.aCnt)

这有2个子查询,每个子查询生成一个从0到9的数字范围,这些数字被操作以给出0到99.