根据数量金额重复行

时间:2013-12-09 10:47:28

标签: mysql duplicates

美好的一天

我有一个名为PO的表,它看起来像这样:

ID | ITEM | Description | Quantity | Amount |
----------------------------------------------
1  | A123 | Item 1      |    2     |  500   |
2  | A676 | Item 2      |    3     |  300   |

有没有办法根据Quantity amount and Number来复制该行(如View)。

ID  | ITEM | Description | Quantity | Amount | Number |
-------------------------------------------------------    
1   | A123 | Item1       |    2     |  500   |   1    |
1   | A123 | Item1       |    2     |  500   |   2    |
2   | A676 | Item2       |    3     |  300   |   1    |
2   | A676 | Item2       |    3     |  300   |   2    |
2   | A676 | Item2       |    3     |  300   |   3    |

由于

1 个答案:

答案 0 :(得分:1)

你可以这样做:

select
t.*,
numbers.n
from
t
inner join 
(
 select aa.a + 10 * bb.b + 100 * cc.c + 1000 * dd.d as n from
 (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) aa,
 (select 0 as b union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) bb,
 (select 0 as c union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) cc,
 (select 0 as d union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) dd
) numbers
on t.Quantity >= numbers.n
where numbers.n != 0
order by t.id, numbers.n

这看起来有点复杂,因为我创建了一个表格,其中包含0到99之间的数字。最好有一张能够满足你需要的数量的表格并加入到这个数字中。