MySQL: Multiple Randomly Selected Lowercase Letters

This generates a single lowercase letter:
SELECT CHAR(FLOOR(RAND()*26)+97)

Open in new window

Using MySQL, how can I generate 10 random lowercase letters?
LVL 10
skijAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
Side by side?
SELECT CHAR(FLOOR(RAND()*26)+97) L0
, CHAR(FLOOR(RAND()*26)+97) L1
, CHAR(FLOOR(RAND()*26)+97) L2
, CHAR(FLOOR(RAND()*26)+97) L3
, CHAR(FLOOR(RAND()*26)+97) L4
, CHAR(FLOOR(RAND()*26)+97) L5
, CHAR(FLOOR(RAND()*26)+97) L6
, CHAR(FLOOR(RAND()*26)+97) L7
, CHAR(FLOOR(RAND()*26)+97) L8
, CHAR(FLOOR(RAND()*26)+97) L9

Open in new window


One per row (UNION ALL allows duplicates):
SELECT CHAR(FLOOR(RAND()*26)+97) as letter
UNION ALL
SELECT CHAR(FLOOR(RAND()*26)+97)
UNION ALL
SELECT CHAR(FLOOR(RAND()*26)+97)
UNION ALL
SELECT CHAR(FLOOR(RAND()*26)+97)
UNION ALL
SELECT CHAR(FLOOR(RAND()*26)+97)
UNION ALL
SELECT CHAR(FLOOR(RAND()*26)+97)
UNION ALL
SELECT CHAR(FLOOR(RAND()*26)+97)
UNION ALL
SELECT CHAR(FLOOR(RAND()*26)+97)
UNION ALL
SELECT CHAR(FLOOR(RAND()*26)+97)
UNION ALL
SELECT CHAR(FLOOR(RAND()*26)+97)

Open in new window


Without ALL you can get unique but you may need to do more than 10 UNIONs due to collisions.
SELECT * FROM (
SELECT CHAR(FLOOR(RAND()*26)+97) as letter
UNION
SELECT CHAR(FLOOR(RAND()*26)+97)
UNION
SELECT CHAR(FLOOR(RAND()*26)+97)
UNION
SELECT CHAR(FLOOR(RAND()*26)+97)
UNION
SELECT CHAR(FLOOR(RAND()*26)+97)
UNION
SELECT CHAR(FLOOR(RAND()*26)+97)
UNION
SELECT CHAR(FLOOR(RAND()*26)+97)
UNION
SELECT CHAR(FLOOR(RAND()*26)+97)
UNION
SELECT CHAR(FLOOR(RAND()*26)+97)
UNION
SELECT CHAR(FLOOR(RAND()*26)+97)
UNION
SELECT CHAR(FLOOR(RAND()*26)+97)
UNION
SELECT CHAR(FLOOR(RAND()*26)+97) ) as t
LIMIT 10; -- this is what limits the result to 10, even though you have more than 10 select statements

Open in new window

skijAuthor Commented:
I don't want them side by side or in seporate rows.  I want a single result, like this:
select CONCAT(CHAR(FLOOR(RAND()*26)+97),CHAR(FLOOR(RAND()*26)+97),CHAR(FLOOR(RAND()*26)+97),CHAR(FLOOR(RAND()*26)+97),CHAR(FLOOR(RAND()*26)+97),CHAR(FLOOR(RAND()*26)+97),CHAR(FLOOR(RAND()*26)+97),CHAR(FLOOR(RAND()*26)+97),CHAR(FLOOR(RAND()*26)+97),CHAR(FLOOR(RAND()*26)+97)) as `RndLetters`

Open in new window

That returns what I want, however using CONCAT seems excessive here.    I was hoping for a way to do this less code.  I thought there might be a creative way like this (which doesn't actually work...)
SELECT CAST(CHAR(FLOOR(RAND()*1000000000)+999999999) AS CHAR CHARACTER SET ascii)

Open in new window

theGhost_k8Database ConsultantCommented:
I wrote random data generator for MySQL long back, I'd copy the function of get_string from it and modify a bit to the requirement:

You can use following function and pass the string-length as an argument to get what you want:
## MySQL function to generate random string of specified length
DROP function if exists get_string;
delimiter $$
CREATE FUNCTION get_string(in_strlen int) RETURNS VARCHAR(500) DETERMINISTIC
BEGIN
set @var:='';
while(in_strlen>0) do
set @var:=concat(@var,IFNULL(ELT(1+FLOOR(RAND() * 26), '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'),'anytext'));
set in_strlen:=in_strlen-1;
end while;
RETURN @var;
END $$
delimiter ;

Open in new window


[
Is this far more code now? As such what you/user will have to write is only a call to the function, which will appear smaller :)
select get_string(10);

Open in new window

]

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.