Home>Article>Database> How to use sum function in mysql

How to use sum function in mysql

anonymity
anonymity Original
2019-05-09 15:08:22 7067browse

How to use sum function in mysql

MySQL SUM() function is used to calculate the sum of a set of values or expressions. The syntax of the SUM() function is as follows:

SUM(DISTINCT expression)

SUM() How do functions work?

If you use the SUM function in a SELECT statement that does not return matching rows, the SUM function returns NULL instead of 0. The DISTINCT operator allows counting distinct values in a collection. The SUM function ignores NULL values in calculations.

MySQL SUM() function example

Let’s take a look at the orderdetails table in the sample database (yiibaidb).

You can use the SUM() function to calculate the total amount of order number 10100, as shown in the following query:

SELECT FORMAT(SUM(quantityOrdered * priceEach),2) total FROM orderdetails WHERE orderNumber = 10100;

Execute the above query statement and get the following results

mysql> SELECT FORMAT(SUM(quantityOrdered * priceEach),2) total FROM orderdetails WHERE orderNumber = 10100; +-----------+ | total | +-----------+ | 10,223.83 | +-----------+ 1 row in set SQL

Please note , the FORMAT() function is used to format the return value of the SUM() function.

The above is the detailed content of How to use sum function in mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn