- 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 write a valid MySQL query and update with a custom variable?
Let us first create a table −
mysql> create table DemoTable2027 -> ( -> UserId int -> ); Query OK, 0 rows affected (0.65 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2027 values(10); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable2027 values(20); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable2027 values(31); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable2027 values(11); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable2027;
This will produce the following output −
+--------+ | UserId | +--------+ | 10 | | 20 | | 31 | | 11 | +--------+ 4 rows in set (0.00 sec)
Here is the query to write a valid query and update with a custom variable −
mysql> set @uId:=100; Query OK, 0 rows affected (0.00 sec) mysql> update DemoTable2027 -> set UserId=(@uId:=@uId+10); Query OK, 4 rows affected (0.13 sec) Rows matched: 4 Changed: 4 Warnings: 0
Let us check the table records. −
mysql> select *from DemoTable2027;
This will produce the following output −
+--------+ | UserId | +--------+ | 110 | | 120 | | 130 | | 140 | +--------+ 4 rows in set (0.00 sec)
- Related Articles
- How to bulk update MySQL data with a single query?
- MySQL UPDATE query where id is highest AND field is equal to variable?
- Update two columns with a single MySQL query
- MongoDB query to increment a specific value using custom variable
- MySQL query to find a value in a set of values separated by comma in a custom variable
- Set MySQL select in a custom variable
- How to declare a variable in MySQL for a normal query?
- Update all the zero values with a custom value in MySQL with a function similar to ISNULL()
- How to store Query Result in a variable using MySQL?
- How to assign the result of a MySQL query into a variable?
- Set custom messages by working with MySQL IF Statements and SELECT in a user-defined variable
- MySQL query to update a specific cell to be empty
- MySQL update multiple records in a single query?
- MySQL query to update different fields based on a condition?
- MySQL update query to remove spaces?

Advertisements