How to use sum function in mysql

anonymity
Release: 2020-09-16 14:45:13
Original
7050 people have browsed it

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)
Copy after login

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;
Copy after login

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!