
- 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
Get the sum of multiple row (not all) values from a MySQL table?
You can use aggregate function SUM() from MySQL for this. Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Amount int ); Query OK, 0 rows affected (0.65 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Amount) values(400); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(Amount) values(10); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(Amount) values(50); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Amount) values(500); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Amount) values(80); Query OK, 1 row affected (0.09 sec)
Following is the query to display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+--------+ | Id | Amount | +----+--------+ | 1 | 400 | | 2 | 10 | | 3 | 50 | | 4 | 500 | | 5 | 80 | +----+--------+ 5 rows in set (0.00 sec)
Here is the query to get the sum of rows (not all) out of MySQL table −
mysql> select sum(Amount) from DemoTable where Id in(1,4,5);
This will produce the following output −
+-------------+ | sum(Amount) | +-------------+ | 980 | +-------------+ 1 row in set (0.00 sec)
- Related Articles
- Get the sum of last 3 digits from all the values in a column with MySQL
- Get the average row length of a MySQL table
- Sum values of a single row in MySQL?
- Get values from all rows and display it a single row separated by comma with MySQL
- Get the second last row of a table in MySQL?
- Display random row from a MySQL table
- Display sum in last row of table using MySQL?
- How can I create a MySQL stored procedure that returns multiple values from a MySQL table?
- How to select all the records except a row with certain id from a MySQL table?
- Delete multiple entries from a MySQL table
- Get the sum of a column in all MySQL rows?
- MySQL query to get first two highest column values from a table?
- A single MySQL query to insert records (not all) in the second table from the first table
- MySQL query to select all the records only from a specific column of a table with multiple columns
- Update multiple values in a table with MySQL IF Statement

Advertisements