- 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
Update the table by calculating the sum and display the result as last column value
Use a variable to store SUM(total) and update it with the UPDATE command. Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Value int ); Query OK, 0 rows affected (0.61 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Value) values(70); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(Value) values(100); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(Value) values(150); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(Value) values(250); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Value) values(60); Query OK, 1 row affected (0.22 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+-------+ | Id | Value | +----+-------+ | 1 | 70 | | 2 | 100 | | 3 | 150 | | 4 | 250 | | 5 | 60 | +----+-------+ 5 rows in set (0.00 sec)
Following is the query to set the sum in a variable and display the result as last column value −
mysql> set @Total=(select sum(Value) from DemoTable); Query OK, 0 rows affected (0.00 sec) mysql> update DemoTable set Value= @Total where Id=5; Query OK, 1 row affected (0.16 sec) Rows matched: 1 Changed: 1 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable;
This will produce the following output −
+----+-------+ | Id | Value | +----+-------+ | 1 | 70 | | 2 | 100 | | 3 | 150 | | 4 | 250 | | 5 | 630 | +----+-------+ 5 rows in set (0.00 sec)
- Related Articles
- How to group by column name and ensure the query retrieves the last update in MySQL?
- Update a table in MySQL and display only the initials name in a new column
- Complete the last column of the table.
- Get the minimum and maximum value from a VARCHAR column and display the result in separate MySQL columns?
- Display sum in last row of table using MySQL?
- Count number of occurrences of records in a MySQL table and display the result in a new column?
- MySQL query to group by column and display the sum of similar values in another column
- MySQL query to sum the Product Price values from similar columns for same customers and display the result in the same column
- JavaScript display the result of a function as HTML?
- Setting column values as column names in the MySQL query result?
- Count duplicate ids and display the result in a separate column with MySQL
- Display the count of duplicate records from a column in MySQL and order the result
- How to round MySQL column with float values and display the result in a new column?
- How can we update any value in MySQL view as we can update the values in MySQL table?
- Calculating percentage in a MySQL query and round off the result

Advertisements