【问题标题】:Repeat rows in the result based on an integer value in column根据列中的整数值重复结果中的行
【发布时间】:2018-11-21 12:56:12
【问题描述】:

我有下表。

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

【问题讨论】:

  • 这不是你应该在客户端做的吗?
  • 这样做的目的是什么?您可以使用数字生成器表。
  • 看起来我只使用这种方法找到了解决方案。

标签: mysql sql database common-table-expression recursive-query


【解决方案1】:

我认为,最好不要使用查询(SQL)来解决。 有一些生成功能,但是性能很差。

您必须更改模型(始终存储 1 个数量),或在后端处理它(java/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...数量-1 值重复连接。

【讨论】:

  • 请添加说明。还建议避免使用基于逗号的连接,而是使用现代 JOIN ..ON 语法。 +1
  • 谢谢。我用加入和一些解释重新编辑了它
  • 我认为这个解决方案行不通,我不确定这是通用解决方案还是仅此而已。我可以想到一个具有存储过程的解决方案,但我想看看我是否可以有一个带有选择查询的解决方案。
  • 如果将计数插入一个表并加入该表,可能会好一些。但如果 99999>=quantity>=0 则有效。
【解决方案2】:

在参考了如何在 mysql 中生成系列的答案后,我能够为我的问题提出解决方案。这里是the link

SELECT
  sal.id,
  1 as quantity, sal.quantity as originalQty,
  sal.price_charged/sal.quantity
FROM
  (SELECT
     @num := @num + 1 AS count
   FROM
     sales, -- this can be any table but it should have row count 
            -- more than what we expect the max value of Sales.quantity column
     (SELECT @num := 0) num
   LIMIT
     100) ctr
  JOIN sales sal
    ON sal.quantity >= ctr.count
    order by id;

【讨论】:

    【解决方案3】:

    如果你有幸运行 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;
    

    this DB Fiddle 和您的示例数据中,查询返回:

    | id  | quantity | quantity |
    | --- | -------- | -------- |
    | 101 | 2        | 50       |
    | 101 | 2        | 50       |
    | 102 | 3        | 100      |
    | 102 | 3        | 100      |
    | 102 | 3        | 100      |
    | 103 | 1        | 120      |
    

    【讨论】:

      猜你喜欢
      • 2016-11-24
      • 2013-04-04
      • 2021-05-04
      • 2017-12-24
      • 1970-01-01
      • 2021-12-06
      • 2016-06-27
      • 1970-01-01
      • 2019-02-14
      相关资源
      最近更新 更多