Speedychuck Speedychuck - 4 months ago 19
SQL Question

SQL insert using RAND() for inserting random value - INSERT into TINYINT?

I have a MySQL Insert and I am using

RAND
to generate a random
INT
value the issue is it wont insert into the DB as the column accepts
TINYINT
, How can I convert the output to
TINYINT
. Code example Below:

INSERT INTO table (`col`, `col` VALUES (NULL, CAST(FLOOR(5+RAND()*(1-5)) AS CHAR);


So I want to randomly insert a number between 1 and 5 which works, however, because the column is set to
TINYINT
and not
INT
i get 1592 unsafe statement. And the DB sets the col to its default value 1.

How can I use
RAND
to enter a random value through using an
INSERT INTO
a
TINYINT
col?

Answer
INSERT INTO table (`col1`, `col2`)
VALUES
    (NULL, FLOOR(1 + RAND( )*5));