多次将字符串插入 mysql,每次删除最后一个字符,一次查询
Insert string into mysql multiple times, removing last character each time, with one query
使用 MySQL,我希望编写一个插入查询,它将接受一个字符串并将其插入多次,每次删除最后一个字符。
所以查询应该是这样的
INSERT INTO table (str) VALUES ("string") .....
并会导致插入以下值
string
strin
stri
str
st
s
我可以这样做 PHP,但我想知道是否有 SQL 解决方案。
如果你有 table 个号码,你可以这样做:
insert into table(str)
select left(@str, n.n)
from (select 1 as n union all select 2 union all select 3 union all select 4 union all
select 5 union all select 6 union all select 7 union all select 8 union all select 9
) n
where length(@str) >= n.n
order by n.n desc;
此方法要求您创建另一个 table 并用数字预先填充它,但如果您必须重复这样做,这很容易做到并且很有意义。我刚刚在 SQLFiddle
中对此进行了测试
create table table1 (string1 varchar(10));
create table table2 (number1 integer);
insert into table2 values (1);
insert into table2 values (2);
insert into table2 values (3);
insert into table2 values (4);
insert into table2 values (5);
insert into table2 values (6);
insert into table2 values (7);
insert into table2 values (8);
insert into table2 values (9);
insert into table2 values (10);
insert into table1 (string1)
select left(stringval, number1)
from (select 'ninechars' as stringval ) as a
join table2 on table2.number1 <= char_length(a.stringval)
select * from table1
STRING1
n
ni
nin
nine
ninec
ninech
ninecha
ninechar
ninechars
当然,table2 在这种情况下必须有足够的行来满足您需要插入的字符串的最大长度。
最简单的方法是编写一个存储过程,然后调用它来执行插入操作。
DELIMITER #
CREATE PROCEDURE 'insert_string_rows' (IN 'str' text)
BEGIN
DECLARE len int unsigned;
SET len = CHAR_LENGTH(str);
WHILE len > 0 DO
INSERT INTO table VALUES (str);
SET str = SUBSTR(str, 1, len - 1);
SET len = CHAR_LENGTH(str);
END WHILE;
END#
DELIMITER ;
那就干脆
CALL insert_string_rows ("string")
and out 将所有行弹出到 'table'
string
strin
stri
str
st
s
使用 MySQL,我希望编写一个插入查询,它将接受一个字符串并将其插入多次,每次删除最后一个字符。
所以查询应该是这样的
INSERT INTO table (str) VALUES ("string") .....
并会导致插入以下值
string
strin
stri
str
st
s
我可以这样做 PHP,但我想知道是否有 SQL 解决方案。
如果你有 table 个号码,你可以这样做:
insert into table(str)
select left(@str, n.n)
from (select 1 as n union all select 2 union all select 3 union all select 4 union all
select 5 union all select 6 union all select 7 union all select 8 union all select 9
) n
where length(@str) >= n.n
order by n.n desc;
此方法要求您创建另一个 table 并用数字预先填充它,但如果您必须重复这样做,这很容易做到并且很有意义。我刚刚在 SQLFiddle
中对此进行了测试create table table1 (string1 varchar(10));
create table table2 (number1 integer);
insert into table2 values (1);
insert into table2 values (2);
insert into table2 values (3);
insert into table2 values (4);
insert into table2 values (5);
insert into table2 values (6);
insert into table2 values (7);
insert into table2 values (8);
insert into table2 values (9);
insert into table2 values (10);
insert into table1 (string1)
select left(stringval, number1)
from (select 'ninechars' as stringval ) as a
join table2 on table2.number1 <= char_length(a.stringval)
select * from table1
STRING1
n
ni
nin
nine
ninec
ninech
ninecha
ninechar
ninechars
当然,table2 在这种情况下必须有足够的行来满足您需要插入的字符串的最大长度。
最简单的方法是编写一个存储过程,然后调用它来执行插入操作。
DELIMITER #
CREATE PROCEDURE 'insert_string_rows' (IN 'str' text)
BEGIN
DECLARE len int unsigned;
SET len = CHAR_LENGTH(str);
WHILE len > 0 DO
INSERT INTO table VALUES (str);
SET str = SUBSTR(str, 1, len - 1);
SET len = CHAR_LENGTH(str);
END WHILE;
END#
DELIMITER ;
那就干脆
CALL insert_string_rows ("string")
and out 将所有行弹出到 'table'
string
strin
stri
str
st
s