2014-11-24 137 views
-1

有了一個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  | 
+----------+------------+ 
+0

這是很難理解這確實意味着。你能解釋一下嗎? – 2014-11-24 09:01:47

+1

如果蘋果的數量是3,他/她最後想要3行和1個蘋果。 – Veve 2014-11-24 09:03:07

+0

是的,正確的。我想把所有的價值分成一個。 – OCWO 2014-11-24 09:12:19

回答

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

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) 
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