- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to update MySQL column with random value?
To update column with random value, you can use the below syntax−
update yourTableName set yourColumnName =round(1+rand()*100);
The above syntax will generate a value between 1 to 100. Let us see an example and create a table−
mysql> create table DemoTable ( Number int ); Query OK, 0 rows affected (0.46 sec)
Insert records in the table using insert command −
mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(80); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(100000); Query OK, 1 row affected (0.39 sec) mysql> insert into DemoTable values(987); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output−
+--------+ | Number | +--------+ | 10 | | 100 | | 80 | | 100000 | | 987 | +--------+ 5 rows in set (0.00 sec)
Following is the query to update column with random value−
mysql> update DemoTable set Number=round(1+rand()*100); Query OK, 5 rows affected (0.46 sec) Rows matched: 5 Changed: 5 Warnings: 0
Let us display table records once again−
mysql> select *from DemoTable;
This will produce the following output −
+--------+ | Number | +--------+ | 33 | | 88 | | 41 | | 39 | | 70 | +--------+ 5 rows in set (0.00 sec)
- Related Articles
- How to update records in a column with random numbers in MySQL?
- MySQL UPDATE the corresponding column with random number between 1-3?
- How to update a MySQL column by subtracting a value with some conditions?
- Order randomly in MySQL with a random value column?
- MySQL update with random number between 1 - 3
- How to update a specific column value fetched with CASE statement?
- Update only a single column value in MySQL
- Update a MySQL column with JSON format?
- How to update a MySQL date type column?
- Update a column of text with MySQL REPLACE()
- How to update a column with the same values (not all) using UPDATE, SET and LIMIT in MySQL?
- Update a column value, replacing part of a string in MySQL?
- How to update all the entries except a single value in a particular column using MySQL?
- How to cast and update a numeric value from string column only where applicable in MySQL?
- How to update field to add value to existing value in MySQL?

Advertisements