简体   繁体   中英

How to use $RANDOM (linux) in a mySQL command-line query?

I am trying to insert random values into a table from my linux terminal, but when i use the following SQL statement,

INSERT INTO kCreate (k1 , k2) VALUES ('$RANDOM' , '$RANDOM');

where k1 and k2 are of datatype INT, 0 is being inserted instead of a random value, What am i doing wrong here ?

k1 and k2 are of INT type, no need to put the value inside single quote, try this:

sql="INSERT into kcreate ( k1, k2) values ($RANDOM, $RANDOM);"
echo $sql | mysql -ppassword test

You can use rand() function of mysql with ceil(). Here is an example.

INSERT INTO kCreate (k1 , k2) 
  VALUES (ceil(rand()*1000) , ceil(rand()*1000));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM