1

I want to do an INSER INTO statement but i need some fields to generate random numbers each time: Example

INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,RAND NUMBER,RAND NUMBER,...);

So each time it enters a random number into columns when creating a new record. SO most values will be static but some need to be RAND between a number set for example 1 -5 or 1000 - 5000.

Any help?

3
  • This link shows you how to use RAND() to generate a random number within a range. You can just plug this into your INSERT statement. Commented Jul 22, 2016 at 14:30
  • I tried that so in the values I have VALUES ('511222', RAND()*(5-1)+1, '323632') Because I want numbers between 1 and 5. using MYSQL work bench not sure if that makes a difference Commented Jul 22, 2016 at 14:31
  • @Speedychuck did that not work ? Commented Jul 22, 2016 at 14:46

3 Answers 3

2

Using this function should work:

FLOOR(min+RAND()*(max-min))

INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,SELECT FLOOR(5+RAND()*(1-5)),SELECT FLOOR(5+RAND()*(1-5)),...);
Sign up to request clarification or add additional context in comments.

8 Comments

How would this look in the insert statement? @Zoidburg
Just tried this: getting a 1582 incorrect parameter count in call to native function floor.
Need an extra ) after (10-100)
Ok this worked, however its inserting as 0 I have (1-5) should it not be between 1 and 5 that is generated?
Ok its because the db only accepts numbers as strings grrr! I need to populate that field with a number between 1 and 5 which is entered as a string how can I fix this?
|
1

Insert some rows with random numbers from 1 to 5 in some columns.

Schema:

create table friday1
(   id int auto_increment primary key,
    value1 int not null,
    value2 int not null
);

Insert 3 rows:

insert friday1(value1,value2) select floor(rand()*5)+1,floor(rand()*5)+1;
insert friday1(value1,value2) select floor(rand()*5)+1,floor(rand()*5)+1;
insert friday1(value1,value2) select floor(rand()*5)+1,floor(rand()*5)+1;

Stored Proc helper:

drop procedure if exists insertMany;
DELIMITER $$
create procedure insertMany
(   howMany int
)
BEGIN
    DECLARE soFar int default 0;

    set howMany=least(howMany,500); -- max out at 500 regardless of IN parameter (RAND is slow)
    WHILE soFar<howMany DO
        insert friday1(value1,value2) select floor(rand()*5)+1,floor(rand()*5)+1;
        set soFar=soFar+1;
    END WHILE;
    select soFar; -- # of times in loop
END$$
DELIMITER ; 

Test:

call insertMany(200);

Results:

select count(*) as theCount, 
least(min(value1),min(value2)) as theMin, 
greatest(max(value1),max(value2)) as theMax 
from friday1; 

+----------+--------+--------+
| theCount | theMin | theMax |
+----------+--------+--------+
|      203 |      1 |      5 |
+----------+--------+--------+

Comments

0

Take a look at this.

You can use RAND(), but as it says in the documentation,

If seed is not specified, the SQL Server Database Engine assigns a seed value at random. For a specified seed value, the result returned is always the same.

Hope this helps you

EDIT:

So if you want to use it in the INSERT INTO....i assume

INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, RAND(CAST( NEWID() AS varbinary )) * 5, RAND(CAST( NEWID() AS varbinary )) * 1000,...)

If this doesnt help, thumbs down as I'm going from what I'd try first and im not testing

4 Comments

Need to know how to use it in an INSERT INTO :( so when I use the insert it will Generate the number between 1 and 5 for that column
Look at my edit, try it out....youll need to round the number if you want a whole number
CAST(FLOOR(5+RAND()*(1-5)) AS CHAR) Now works, however when I check what the column accepts it is a TINYINT. So FLOOR RAND generates the number between 1 and 5 but wont insert it into the column becasue its trying to add an INT which is then converted into a String. How do I add as TINYINT is my issues. @Alex Grogan
Do you really want to use a TINYINT? It only has a range of 0-255? You're betting off changing the design of the table so its an INT instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.