
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to add a random number between 30 and 300 to an existing field in MySQL?
Let us first create a demo table
mysql> create table RandomNumberDemo -> ( -> MyNumber int -> ); Query OK, 0 rows affected (0.54 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into RandomNumberDemo values(17); Query OK, 1 row affected (0.20 sec) mysql> insert into RandomNumberDemo values(18); Query OK, 1 row affected (0.12 sec) mysql> insert into RandomNumberDemo values(29); Query OK, 1 row affected (0.49 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from RandomNumberDemo;
The following is the output
+----------+ | MyNumber | +----------+ | 17 | | 18 | | 29 | +----------+ 3 rows in set (0.00 sec)
Here is the query to add a random number between 30 and 300 to an existing field
mysql> update RandomNumberDemo set MyNumber=MyNumber+ FLOOR(RAND() * (270 + 1)) + 30 where MyNumber=18; Query OK, 1 row affected (0.20 sec) Rows matched: 1 Changed: 1 Warnings: 0
Let us check the table records once again. The query is as follows −
mysql> select *from RandomNumberDemo;
The following is the output
+----------+ | MyNumber | +----------+ | 17 | | 158 | | 29 | +----------+ 3 rows in set (0.00 sec)
- Related Articles
- How to update field to add value to existing value in MySQL?
- How to make an existing field Unique in MySQL?
- How can we add a FOREIGN KEY constraint to the field of an existing MySQL table?
- How to add columns to an existing MySQL table?
- How to add current date to an existing MySQL table?
- How do I add a field to existing record in MongoDB?
- How to add a field with specific datatype (list, object) in an existing MongoDB document?
- Making an existing field Unique in MySQL?
- How to add +1 to existing MySQL values?
- Add data to existing data in a MySQL Database?
- How to add not null constraint to existing column in MySQL?
- How to add properties and methods to an existing object in JavaScript?
- How to add 30 minutes to datetime in MySQL?
- How to add column to an existing table in PostgreSQL?
- How to add a day to datetime field in MySQL query?

Advertisements