
- 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
How to display two different sums of the same price from column Amount in MySQL?
For this, you can use case statement. Let us first create a table −
mysql> create table DemoTable1794 ( Amount int ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1794 values(100); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1794 values(80); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1794 values(320); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1794;
This will produce the following output −
+--------+ | Amount | +--------+ | 100 | | 80 | | 320 | +--------+ 3 rows in set (0.00 sec)
Following is the query to display two different sums of the same price from column Amount −
mysql> select sum(Amount), sum( case when Amount < 150 then Amount else 0 end ) as SecondAmount from DemoTable1794;
This will produce the following output −
+-------------+--------------+ | sum(Amount) | SecondAmount | +-------------+--------------+ | 500 | 180 | +-------------+--------------+ 1 row in set (0.00 sec)
- Related Articles
- How to select different values from same column and display them in different columns with MySQL?
- Concatenate two values from the same column with different conditions in MySQL
- MySQL query to sum the Product Price values from similar columns for same customers and display the result in the same column
- How to subtract the same amount from all values in a column with MySQL?
- Copy from one column to another (different tables same database) in MySQL?
- How to select and display a list of values in one column that are available in two different MySQL columns?
- Display highest amount from corresponding duplicate ids in MySQL
- How to display the column names from a table excluding some in MySQL?
- How to display the count from distinct records in the same row with MySQL?
- How to display data from a MySQL column separated by comma?
- Display two different columns from two different tables with ORDER BY?
- Select distinct names from two columns in MySQL and display the result in a single column
- Divide numbers from two columns and display result in a new column with MySQL
- How come two children from the same family have different nature?
- MySQL query to count the number of 0s and 1s from a table column and display them in two columns?

Advertisements