Mysql 根据列中的整数值在结果中重复行

Mysql 根据列中的整数值在结果中重复行,mysql,sql,database,Mysql,Sql,Database,我有下表 Sales: id quantity price_charged ------------------------------ 101 2 100 102 3 300 103 1 120 我希望选择记录,使其根据数量列值重复N行 所以我需要以下结果 id quantity price_charged -------------------------

我有下表

Sales:
id      quantity    price_charged
------------------------------
101         2           100
102         3           300
103         1           120
我希望选择记录,使其根据数量列值重复N行

所以我需要以下结果

id    quantity    price_charged
--------------------------------
101     1          50
101     1          50
102     1          100
102     1          100
102     1          100
103     1          120

我认为,最好不要使用querySQL来解决。 有一些生成功能,但其性能较差

您必须将modelstore始终更改为1个数量,或者在backendjava/c/stb中处理它

Select id, 
       1 as quantity, 
       price_charged 
from table_name t
JOIN 
(SELECT e*10000+d*1000+c*100+b*10+a n FROM
(select 0 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) t1,
(select 0 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) t2,
(select 0 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) t3,
(select 0 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) t4,
(select 0 e 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) t5) counter
ON (counter.n<=t.quantity)

联接的子查询重新分配了0到99999之间的数字,这是数量的最大值。计数器0正在重复连接。。。quantity-1值。

在参考了如何在mysql中生成序列的答案后,我能够为我的问题找到解决方案。这是


如果您足够幸运地运行MySQL 8.0,您可以使用递归CTE来解决此问题。这是一个优雅的解决方案,不需要创建使用变量的数量列表

考虑这个查询:

WITH RECURSIVE cte AS (
      SELECT 1 n, id, quantity, price_charged FROM sales
      UNION ALL
      SELECT n + 1, id, quantity, price_charged FROM cte WHERE n < quantity
)
SELECT id, quantity, price_charged/quantity quantity
FROM cte
ORDER BY id;

这不是你应该在客户身上做的事情吗?这是为了什么?您可以使用数字生成器表。看起来我仅使用此方法找到了解决方案。请添加说明。此外,建议避免使用基于逗号的联接,而是使用语法上的现代联接+谢谢你。我用join和一些解释重新编辑了它。我认为这个解决方案行不通,我不确定这是一个通用的解决方案还是一次性的。我可以想到一个具有存储过程的解决方案,但我想看看是否可以使用select查询解决方案。如果将计数插入一个表并连接到该表,可能会更好一些。但如果99999>=数量>=0,则该方法有效。
WITH RECURSIVE cte AS (
      SELECT 1 n, id, quantity, price_charged FROM sales
      UNION ALL
      SELECT n + 1, id, quantity, price_charged FROM cte WHERE n < quantity
)
SELECT id, quantity, price_charged/quantity quantity
FROM cte
ORDER BY id;
| id  | quantity | quantity |
| --- | -------- | -------- |
| 101 | 2        | 50       |
| 101 | 2        | 50       |
| 102 | 3        | 100      |
| 102 | 3        | 100      |
| 102 | 3        | 100      |
| 103 | 1        | 120      |