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
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.
The sql procedure I wrote worked.. It took around 4-5 hours to popoulta 26x26x26x26 combinations of 6 string words.
Thanks for all the suggestions !
Related
I'm having a bit of trouble. I need to award an item to users on our site, but I don't want to manually fill in the numbers one by one. Is there a way to set the SQL query to INSERT INTO from UID 9 to 5430 without having to create multiple lines? Here's my example.
INSERT INTO `item_owned` (`id`, `uid`, `iid`, `kind`, `time_owned`, `notes`) VALUES (NULL, 'x', '3626', '1', '1592596732', 'NotBanned')
I'm trying to have the "x" be a number, but to have MYSQL generate multiple numbers from 9 to 5430 without having to generate multiple numbers/code all at once. So something like:
INSERT INTO `item_owned` (`id`, `uid`, `iid`, `kind`, `time_owned`, `notes`) VALUES (NULL, '9 - 5430', '3626', '1', '1592596732', 'NotBanned')
The 9 - 5430 is where the issue is. I want to award the item to everyone who has their number between the number 9 and 5430.
Help appreciated - thanks.
You can use stored procedure in mysql to do this; inside your stored procedure you can use a loop to insert multiple entries. I've provided an example below.
Procedure can be implemented like the code shown here:
delimiter $$
create procedure fill_rows(in start_index int,in termination_point int)
begin
while start_index <= termination_point do
INSERT INTO `item_owned` (`id`, `uid`, `iid`, `kind`, `time_owned`, `notes`) VALUES (NULL, start_index, '3626', '1', '1592596732', 'NotBanned');
set start_index := start_index + 1;
end while;
end $$
delimiter ;
Now whenever you want to insert uid let's say from range x to y. Assume x = 10 and y = 1000 then you can simply insert this records using this one time procedure call like:
call fill_row(10, 1000);
This call will insert 990 new rows with uid values 10, 11, 12 ...1000.
Hope this may help you!
I have a variable #ids = "894,891,896,899..."
What I like to do is the following
loop #id: #ids
INSERT INTO `testcases_types` (`testcase_id`, `type_id`) VALUES (#id, '57');
INSERT INTO `testcases_types` (`testcase_id`, `type_id`) VALUES (#id, '58');
INSERT INTO `testcases_types` (`testcase_id`, `type_id`) VALUES (#id, '59');
INSERT INTO `testcases_types` (`testcase_id`, `type_id`) VALUES (#id, '60');
end loop
So I tried a lot now , but I cannot find out the right syntax. I do not want to write a procedure. I just want to execute it once .
create table mixedvalues (value varchar(50));
insert into mixedvalues values ('100');
insert into mixedvalues values ('ABC');
insert into mixedvalues values ('ABC100');
insert into mixedvalues values ('200');
insert into mixedvalues values ('EFEA');
insert into mixedvalues values ('EFEA200');
insert into mixedvalues values ('300');
insert into mixedvalues values ('AAFASF300');
insert into mixedvalues values ('400');
insert into mixedvalues values ('AERG400');
insert into mixedvalues values ('500');
insert into mixedvalues values ('AGE500');
Here i can get alpha value using following query
SELECT * FROM mixedvalues WHERE value REGEXP '^[A-z]+$'
value
ABC
EFEA
and for numeric value
SELECT * FROM mixedvalues WHERE value REGEXP '^-?[0-9]+$'
value
100
200
300
400
500
then how can i get alpha and numeric value?
result should be like
value
ABC100
EFEA200
AAFASF300
AERG400
AGE500
If your data is always alpha then numeric the following should work :
SELECT * FROM mixedvalues WHERE value REGEXP '^[A-Z]+-?[0-9]+$'
Given below is my table structure
CREATE TABLE gtab86
(
mlid integer DEFAULT nextval('seq_gtab86_id'::regclass),
acyrid integer,
lmonth integer,
islocked boolean
)
In this table lmonth is the month fied, and acyrid is the year denoting value for example 1.
I wrote the following Function to insert into gtab86.
CREATE OR REPLACE FUNCTION createmonthlock(iacyrid integer)
RETURNS void AS '
BEGIN
INSERT INTO gtab86(acyrid, lmonth) VALUES (iacyrid,1);
INSERT INTO gtab86(acyrid, lmonth) VALUES (iacyrid,2);
INSERT INTO gtab86(acyrid, lmonth) VALUES (iacyrid,3);
INSERT INTO gtab86(acyrid, lmonth) VALUES (iacyrid,4);
INSERT INTO gtab86(acyrid, lmonth) VALUES (iacyrid,5);
INSERT INTO gtab86(acyrid, lmonth) VALUES (iacyrid,6);
INSERT INTO gtab86(acyrid, lmonth)VALUES (iacyrid,7);
INSERT INTO gtab86(acyrid, lmonth)VALUES (iacyrid,8);
INSERT INTO gtab86(acyrid, lmonth)VALUES (iacyrid,9);
INSERT INTO gtab86(acyrid, lmonth)VALUES (iacyrid,10);
INSERT INTO gtab86(acyrid, lmonth)VALUES (iacyrid,11);
INSERT INTO gtab86(acyrid, lmonth)VALUES (iacyrid,12);
END;'
LANGUAGE plpgsql VOLATILE COST 100;
As you can see, insert into is repeated 12 times (for 12 lmonth field rows).
How to make it only one insert query?
Is it possible to use for 0 to 12 kind things inside a function?
You can use insert .. select based on generate_series().
You also don't need PL/pgSQL for this, a plain SQL function will do:
create or replace function createmonthlock(iacyrid integer)
returns void
as
$body$
INSERT INTO gtab86(acyrid, lmonth)
select iacyrid, num
from generate_series(1,12) num;
$body$
language sql;
You can insert multiple rows with a single INSERT statement:
INSERT INTO gtab86(acyrid, lmonth) VALUES (iacyrid,1), (iacyrid,2), (iacyrid,3), (iacyrid,4), (iacyrid,5), (iacyrid,6), (iacyrid,7), (iacyrid,8), (iacyrid,9), (iacyrid,10), (iacyrid,11), (iacyrid,12);
I need to use user-defined variable in an INSERT query for MySQL, see an example bellow:
INSERT INTO `posts`(`id`) VALUES(NULL);
SET #last_insert_id = LAST_INSERT_ID();
INSERT INTO `comments`(`id`, `post_id`) VALUES(NULL, "#last_insert_id");
This example doesn't work and inserted 0. What am I doing wrong?
There is no need to store it in a variable. You can just call LAST_INSERT_ID() inside the following INSERT statement.
INSERT INTO `comments`(`id`, `post_id`) VALUES (NULL, LAST_INSERT_ID());
... unless you have multiple inserts to perform using that id.
In that case, the proper syntax for using the variable is to do so without quotes:
INSERT INTO `posts`(`id`) VALUES (NULL);
SET #last_insert_id = LAST_INSERT_ID();
/* Several new entries using the same #last_insert_id */
INSERT INTO `comments`(`id`, `post_id`) VALUES (NULL, #last_insert_id);
INSERT INTO `comments`(`id`, `post_id`) VALUES (NULL, #last_insert_id);
INSERT INTO `comments`(`id`, `post_id`) VALUES (NULL, #last_insert_id);
INSERT INTO `comments`(`id`, `post_id`) VALUES (NULL, #last_insert_id);
INSERT INTO `comments`(`id`, `post_id`) VALUES (NULL, #last_insert_id);