select the value which are contain alpha and numeric value in mysql? - mysql

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]+$'

Related

Looping over a SQL result with multiple rows in MySQL

Let's say I have a "source table" which holds some rows, in this case four. Now I'd like to insert new rows into a "target table" for each of the rows from the source table.
My current statement is:
SET #id = 1;
INSERT INTO target_table (id, value)
VALUES (#id, 1),
(#id, 2),
(#id, 3),
(#id, 4);
However I'd like to do something like this...
SET #id = 1;
myResultSet = SELECT value FROM source_table;
FOR EACH value in myResultSet
INSERT INTO target_table (#id, #value)
END
You can use Insert ... Select statement:
INSERT INTO `target_table` (`id`, `value`)
SELECT 1, `value`
FROM `source_table`
Completely possibilities of this statement can be seen from the syntax below:
INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name
[PARTITION (partition_name [, partition_name] ...)]
[(col_name [, col_name] ...)]
SELECT ...
[ON DUPLICATE KEY UPDATE assignment_list]
value:
{expr | DEFAULT}
assignment:
col_name = value
assignment_list:
assignment [, assignment] ...
INSERT INTO target_table ( id, value )
SELECT row_number() OVER (order by <whatever>) , value FROM source_table
or
INSERT INTO target_table( id, value )
SELECT 1, value FROM source_table
depending on what you mean
INSERT INTO target_table( id, value )
SELECT 1, value FROM source_table

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
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 !

Insert string into mysql multiple times, removing last character each time, with one query

Using MySQL, I'm looking to write a single insert query that will take a single string and insert it multiple times, each time removing the last character.
So the query would be something like
INSERT INTO table (str) VALUES ("string") .....
and would result in the following values being inserted
string
strin
stri
str
st
s
I could do this PHP, but I'm wondering if there is an SQL solution first.
This approach requires that you create another table and prepopulate it with numbers, but it's easy to do and makes sense if this is something you will have to do repeatedly. I just tested this in 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
Of course, table2 in this case must have enough rows for the max length of the string you need to insert.
If you have a table of numbers, you could do:
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;
The simplest way is to write a stored procedure and then just call it to do the inserts.
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 ;
Then just simply
CALL insert_string_rows ("string")
and out pops all the rows into 'table'
string
strin
stri
str
st
s

SQL INSERT into MySQL table where column name includes an apostrophe

Does anyone know how I can perform an SQL INSERT operation into a MySQL table where column names include apostrophes, for example:
INSERT INTO MYTABLE (`id`, `Column'1`, `Column'2`) VALUES...
I have tried things like this but to no avail:
INSERT INTO MYTABLE (`id`, `Column''1`, `Column''2`) VALUES...
INSERT INTO MYTABLE (`id`, `Column\'1`, `Column\'2`) VALUES...
You should be able to just place backticks around the column name:
INSERT INTO MYTABLE (`id`, `Column'1`, `Column'2`) VALUES...
see SQL Fiddle with Demo
create table yourtable
(
id int,
col1 varchar(10),
`col'2` varchar(10)
);
insert into yourtable (id, col1, `col'2`) values
(1, 'test', 'sdfsd'),
(1, 'test', 'gtet')
Just put the name into a variable and use the character 39 for the apostrophe.
Example :
L'oiseau becomes :
Loiseau = 'L' + chr(39) + 'oiseau'
print(Loiseau)
=> L'oiseau

User-defined variable in an INSERT query for MySQL

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);