【问题标题】:Sequential Data Generation顺序数据生成
【发布时间】:2015-10-06 10:34:57
【问题描述】:

我想创建 5 个字符串的顺序数据,比如

aaaaa
aaaab
aaaac

....直到

zzzzx
zzzzy
zzzzz

sql 有什么函数可以帮助我生成顺序数据吗?

目前我有四位序列数据,如何生成五位序列数据?

我有什么

aaaa
aaab
aaac

....直到

zzzx
zzzy
zzzz

我编写了以下程序,但它需要很长时间才能完成.. 任何人都可以帮我重写程序或建议不同的方法。

CREATE DEFINER=`root`@`localhost` PROCEDURE `new_procedure`()
BEGIN

      DECLARE a INT Default 1 ;
      DECLARE  tran varchar(255) Default 'aaaa';
      simple_loop: LOOP
         SET a=a+1;
         SET tran = (select fourth from m where idm=a);
         Insert into test.qwe(zxc) values (CONCAT(tran,'a'));
         Insert into test.qwe(zxc) values (CONCAT(tran,'b'));
        Insert into test.qwe(zxc) values (CONCAT(tran,'c'));
         Insert into test.qwe(zxc) values (CONCAT(tran,'d'));
         Insert into test.qwe(zxc) values (CONCAT(tran,'e'));
         Insert into test.qwe(zxc) values (CONCAT(tran,'f'));
         Insert into test.qwe(zxc) values (CONCAT(tran,'g'));
         Insert into test.qwe(zxc) values (CONCAT(tran,'h'));
         Insert into test.qwe(zxc) values (CONCAT(tran,'i'));
         Insert into test.qwe(zxc) values (CONCAT(tran,'j'));
         Insert into test.qwe(zxc) values (CONCAT(tran,'k'));
         Insert into test.qwe(zxc) values (CONCAT(tran,'l'));
         Insert into test.qwe(zxc) values (CONCAT(tran,'m'));
         Insert into test.qwe(zxc) values (CONCAT(tran,'n'));
         Insert into test.qwe(zxc) values (CONCAT(tran,'o'));
         Insert into test.qwe(zxc) values (CONCAT(tran,'p'));
         Insert into test.qwe(zxc) values (CONCAT(tran,'q'));
         Insert into test.qwe(zxc) values (CONCAT(tran,'r'));
         Insert into test.qwe(zxc) values (CONCAT(tran,'s'));
         Insert into test.qwe(zxc) values (CONCAT(tran,'t'));
         Insert into test.qwe(zxc) values (CONCAT(tran,'u'));
         Insert into test.qwe(zxc) values (CONCAT(tran,'v'));
         Insert into test.qwe(zxc) values (CONCAT(tran,'w'));
         Insert into test.qwe(zxc) values (CONCAT(tran,'x'));
         Insert into test.qwe(zxc) values (CONCAT(tran,'y'));
         Insert into test.qwe(zxc) values (CONCAT(tran,'z'));
         IF a=1 THEN
            LEAVE simple_loop;
         END IF;
   END LOOP simple_loop;

END

【问题讨论】:

  • 您必须为此编写自己的代码。尽管可以使您的代码更智能一点,但可以使用 base(26) (a=1, b=2,...,z=26),从 0101010101 (aaaaa) 开始,然后一直加 1 直到您点击2626262626 (zzzzz)
  • 即 11,881,376 行。任何方法保存数据都需要时间。

标签: mysql database mysql-workbench procedure sql-function


【解决方案1】:

从头开始:

CREATE TABLE alpha (a CHAR(1) NOT NULL);

INSERT INTO alpha (a) VALUES
  ('a'), ('b'), ('c'), ('d'), ('e'), ('f'),
  ('g'), ('h'), ('i'), ('j'), ('k'), ('l'),
  ('m'), ('n'), ('o'), ('p'), ('q'), ('r'),
  ('s'), ('t'), ('u'), ('v'), ('w'), ('x'),
  ('y'), ('z');

CREATE TABLE qwe (zxc CHAR(5) NOT NULL);

INSERT INTO qwe (zxc)
SELECT CONCAT(a1.a, a2.a, a3.a, a4.a, a5.a)
FROM alpha a1, alpha a2, alpha a3, alpha a4, alpha a5;

如果您已经在一个表中拥有所有长度为 4 的字符串,您可以只加入一次 alpha 并连接这些值以生成所有长度为 5 的字符串。这仍然需要一些时间,没有办法解决这个问题。

【讨论】:

    【解决方案2】:

    我编写的 sql 程序有效.. 弹出 26x26x26x26 6 个字符串单词的组合大约需要 4-5 小时。

    感谢所有建议!

    【讨论】:

      猜你喜欢
      • 2016-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多