如何在mySQL命令行查询中使用$RANDOM (linux) ?

时间:2022-01-18 15:40:47

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

我试图从linux终端向表中插入随机值,但是当我使用下面的SQL语句时,

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和k2都是数据类型,0是插入而不是随机值,这里我做错了什么?

2 个解决方案

#1


2  

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

k1和k2是INT型,不需要把值放在单引号中,试试:

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

#2


1  

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

您可以使用rand()函数和ceil()。这是一个例子。

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

#1


2  

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

k1和k2是INT型,不需要把值放在单引号中,试试:

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

#2


1  

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

您可以使用rand()函数和ceil()。这是一个例子。

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