Search code examples
mysqldatabasemysql-workbenchproceduresql-function

Sequential Data Generation


I want to to create 5 string sequential data like

aaaaa
aaaab
aaaac

.... upto

zzzzx
zzzzy
zzzzz

Does sql have any function that would help me with sequential data generation?

Currently I have four digits sequential data, how can I make generate five digit sequential data?

What i have

aaaa
aaab
aaac

....upto

zzzx
zzzy
zzzz

I wrote the following procedure but it takes forever to complete.. can anybody help me rewrite the procedure or advise a different approch.

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

Solution

  • From scratch:

    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;
    

    If you already have all the strings of length 4 in a table, you can just join to alpha once and concatenate the values to generate all the strings of length 5. It's still going to take some time, no way around that.