

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL update with random number between 1 - 3
The syntax for updating a column with random number between 1-3 is is as follows −
update yourTableName set yourColumnName=FLOOR(1+RAND()*3);
To understand the above syntax, let us first create a table. The query to create a table is as follows −
mysql> create table UpdateNumber1To3 -> ( -> MyNumber int -> ); Query OK, 0 rows affected (0.61 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into UpdateNumber1To3 values(100); Query OK, 1 row affected (0.16 sec) mysql> insert into UpdateNumber1To3 values(140); Query OK, 1 row affected (0.25 sec) mysql> insert into UpdateNumber1To3 values(130); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from UpdateNumber1To3;
The following is the output −
+----------+ | MyNumber | +----------+ | 100 | | 140 | | 130 | +--------+ 3 rows in set (0.00 sec)
Here is the query to update the MyNumber column values from 1 to 3 −
mysql> update UpdateNumber1To3 set Number=FLOOR(1+RAND()*3); Query OK, 3 rows affected (0.19 sec) Rows matched: 3 Changed: 3 Warnings: 0
Let us check the table once again. The query is as follows −
mysql> SELECT *FROM UpdateNumber1To3;
The following is the output with updated value −
+--------+ | Number | +--------+ | 1 | | 2 | | 1 | +--------+ 3 rows in set (0.00 sec)
- Related Questions & Answers
- MySQL UPDATE the corresponding column with random number between 1-3?
- How to return a random number between 1 and 200 with JavaScript?
- How to update MySQL column with random value?
- Sum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + + (1+3+5+7+....+(2n-1)) in C++
- Sum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + ...... + (1+3+5+7+...+(2n-1)) in C++
- Java Program to generate custom random number -1 or 1
- How to get a pseudo-random number between 0 and 1 in JavaScript?
- How to update records in a column with random numbers in MySQL?
- Count number of trailing zeros in (1^1)*(2^2)*(3^3)*(4^4)*.. in C++
- How to display 3 random values from MySQL table?
- Sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n) in C++
- Sum of the Series 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + ... in C++\n
- Random whole number between two integers JavaScript
- n-th number with digits in {0, 1, 2, 3, 4, 5} in C++
- Sum of the series 1 / 1 + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + … + upto n terms in C++
Advertisements