To select random value, use rand(). Here, we are selecting only a single random value, therefore, LIMIT 1 is used. Following is the syntax −
select *from yourTableName order by rand() limit 1;
Let us first create a table −
mysql> create table DemoTable735 (Amount int); Query OK, 0 rows affected (0.57 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable735 values(984474734); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable735 values(489393323); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable735 values(23432333); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable735 values(8949933); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable735;
This will produce the following output -
+-----------+ | Amount | +-----------+ | 984474734 | | 489393323 | | 23432333 | | 8949933 | +-----------+ 4 rows in set (0.00 sec)
Following is the query to select random row in MySQL
mysql> select *from DemoTable735 order by rand() limit 1;
This will produce the following output -
+-----------+ | Amount | +-----------+ | 489393323 | +-----------+ 1 row in set (0.00 sec)