首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Server:如何创建具有重复序列1-12的列?

Server:如何创建具有重复序列1-12的列?
EN

Stack Overflow用户
提问于 2015-07-02 17:41:22
回答 2查看 2.1K关注 0票数 7

长话短说,我想创建一个列,重复1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,...etc模式。对于(12 * 460343 =) 5524116行。对我如何完成这件事有什么智慧吗?谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-02 17:48:18

插入说48,然后从自己中选择几次。你会很快到达那里的。它比人们想象的要快得多。

如果创建带有int autoinc列的表,则结束时:

代码语言:javascript
运行
复制
delete from table where id>5524116

编辑在这里,你去

代码语言:javascript
运行
复制
    create table idFix
    (   id bigint auto_increment primary key,
        num int not null
    )engine=myisam;

    -- prime it    
    insert into idFix(num) values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12);

    -- this is pretty fast, don't laugh
    -- run the following line 19 times
    insert into idFix(num) select num from idFix;

    -- you now have 6.2m rows (6,291,456)
    select count(*) from idFix

    delete from idFix where id>5524116;

    select count(*) from idFix;

    select min(num),max(num) from idFix;

Takes 3 minutes max

Use your helper table then for the love of Pete drop it !
票数 4
EN

Stack Overflow用户

发布于 2015-07-02 18:19:17

使用一个循环,并对您的计数器做一些模块除法。

代码语言:javascript
运行
复制
DECLARE @LoopCounter bigint
SET @LoopCounter = 0

CREATE TABLE #YourValues
(
    YourValue_Key int NOT NULL identity (1,1) PRIMARY KEY, 
    YourValue_OneThrough12Repating int
)

WHILE @LoopCounter < 5524116
    BEGIN
        INSERT INTO #YourValues (YourValue_OneThrough12Repating) VALUES ((@LoopCounter % 12) + 1)
        SET @LoopCounter = @LoopCounter + 1
    END

SELECT * FROM #YourValues

DROP TABLE #YourValues
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31191189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档