如何通过'a'
生成字符串'zzz'
,然后使用MySQL将它们保存到表中?
答案 0 :(得分:2)
这有点难以从“纯”MySQL中做到,原因有两个:
鉴于这一切,解决方案将如下所示:
CREATE TABLE TEMP_CHARS (
C CHAR PRIMARY KEY
);
INSERT INTO TEMP_CHARS 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');
INSERT INTO YOUR_TABLE(YOUR_FIELD)
SELECT C
FROM TEMP_CHARS
UNION ALL
SELECT CONCAT(T1.C, T2.C) -- Generate all variations (with repetition) of length 2.
FROM TEMP_CHARS T1 CROSS JOIN TEMP_CHARS T2
UNION ALL
SELECT CONCAT(T1.C, T2.C, T3.C) -- Generate all variations (with repetition) of length 3.
FROM TEMP_CHARS T1 CROSS JOIN TEMP_CHARS T2 CROSS JOIN TEMP_CHARS T3;
DROP TABLE TEMP_CHARS;
答案 1 :(得分:0)
如果你知道join = cartesian product,那就很容易了
以下(使用用户定义函数@Branko Dimitrijevic的替代版本),
你可以放入一个文本文件(join.sql): -
create table abc ( a char(3) );
create table a ( a char(1) );
insert into a values ('a','b' ..., 'z');
insert into abc select a from a;
insert into abc
select concat(a.a, b.a) from a
join a as b;
insert into abc
select concat(a.a, b.a, c.a) from a
join a as b
join a as c;
管道进入mysql
mysql -u xxx -h xxx < join.sql