
- 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 sum of column with conditions in MySQL
Let us first create a table −
mysql> create table DemoTable1489 -> ( -> ProductId int, -> ProductPrice int -> ); Query OK, 0 rows affected (0.49 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1489 values(100,900); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1489 values(115,1000); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1489 values(119,2100); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1489 values(125,2100); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1489 values(128,2900); Query OK, 1 row affected (0.25 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1489;
This will produce the following output −
+-----------+--------------+ | ProductId | ProductPrice | +-----------+--------------+ | 100 | 900 | | 115 | 1000 | | 119 | 2100 | | 125 | 2100 | | 128 | 2900 | +-----------+--------------+ 5 rows in set (0.00 sec)
Here is the query to get sum of columns with conditions −
mysql> select ProductId div 10,sum(ProductPrice) from DemoTable1489 -> group by ProductId div 10;
This will produce the following output −
+------------------+-------------------+ | ProductId div 10 | sum(ProductPrice) | +------------------+-------------------+ | 10 | 900 | | 11 | 3100 | | 12 | 5000 | +------------------+-------------------+ 3 rows in set (0.00 sec)
- Related Articles
- Get the sum of a column in all MySQL rows?
- Get the sum of last 3 digits from all the values in a column with MySQL
- Concatenate two values from the same column with different conditions in MySQL
- Set conditions while adding column values in MySQL?
- MySQL query to get sum of each column where every column has same number of values?
- Find sum with MySQL SUM() and give aliases for column heading
- How to update a MySQL column by subtracting a value with some conditions?
- MySQL query to find sum of fields with same column value?
- How to get the sum for every distinct value in another column in MySQL?
- Get the maximum value of a column with MySQL Aggregate function
- Get the substring of a column in MySQL
- Get the maximum count of distinct values in a separate column with MySQL
- Can I get the count of repeated values in a column with MySQL?
- How to get Column name on ResultSet in Java with MySQL?
- Get all the records with two different values in another column with MySQL

Advertisements