首页 > 解决方案 > 如何创建具有增量周数的重复记录

问题描述

我正在尝试从表中创建每条记录的 n 个重复项(例如 5 个),并在与每条记录关联的周数上增加一个增量。

假设有一个表格,其列如下 - week, id 并且有一条记录 - 1, John

我希望 John 的记录重复 5 次以获得 -

1, John

2, John

3, John

4, John

5, John

标签: mysqlsql

解决方案


好吧,您可以生成一个包含五列的表,然后使用它:

select (t.week + x.inc) as week, t.name
from t cross join
     (select 0 as inc union all
      select 1 as inc union all
      select 2 as inc union all
      select 3 as inc union all
      select 4 as inc
     ) x;

推荐阅读