- 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 can I update a field in a MySQL database table by adding a value in the second table with a value from the first table?
Let us first create a table −
mysql> create table DemoTable1 ( value int ); Query OK, 0 rows affected (0.59 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1 values(10); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable1;
Output
+-------+ | value | +-------+ | 10 | +-------+ 1 row in set (0.00 sec)
Following is the query to create second table −
mysql> create table DemoTable2 ( value1 int ); Query OK, 0 rows affected (0.62 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2 values(50); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable2 values(100); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable2;
Output
+--------+ | value1 | +--------+ | 50 | | 100 | +--------+ 2 rows in set (0.00 sec)
Now, update a field in a MySQL database table by adding a value from the first table to a single value in the second table −
mysql> set @myValue=(select value from DemoTable1); Query OK, 0 rows affected (0.00 sec) mysql> update DemoTable2 set value1=value1+@myValue; Query OK, 2 rows affected (0.11 sec) Rows matched: 2 Changed: 2 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable2;
Output
+--------+ | value1 | +--------+ | 60 | | 110 | +--------+ 2 rows in set (0.00 sec)
- Related Articles
- A single MySQL query to select value from first table and insert in the second?
- How can we create a table from an existing MySQL table in the database?
- Update only a single value from a MySQL table where select from same table ordered in descending order?
- Update a table based on StudentId value in MySQL?
- A single MySQL query to insert records (not all) in the second table from the first table
- How can we extract a substring from the value of a column in MySQL table?
- How to update a timestamp field of a MySQL table?
- How can you delete a table from a database in MySQL Python?
- Replace the empty values from a MySQL table with a specific value
- How can I return a record from a table nearest to a user-defined variables value in MySQL?
- How do I view the auto_increment value for a table in MySQL?
- How can we update values in a MySQL table?
- How can I change the value of an instance of a row in MySQL table?
- Using ABAP, changing a value in itab by getting data from database table
- How can we update any value in MySQL view as we can update the values in MySQL table?

Advertisements