Get the App
SLTechnology News&Howtos  ›  Database  › 

How to use the sum function of mysql

Shulou Source: shulou.com Published: 2022-06-01 17:41:50 09月13日 Update

This article mainly tells you how to use the sum function of mysql, related technical terms you can check online or find some related books to supplement, here is not involved, we will go straight to the topic, I hope the sum function of mysql How to use this article can bring you some practical help.

SUM(DISTINCT expression)

How does SUM() function work?

if that SUM function is use in a SELECT statement that returns no match rows, the SUM function returns NULL instead of 0. The DISTINCT operator allows different values in a set to be computed. The SUM function ignores NULL values in calculations.

MySQL SUM() function example

Let's look at the orderdetails table in the sample database (yiibaidb).

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

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

Executing the query above yields 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

Note that the FORMAT() function is used to format the return value of the SUM() function.

How to use the sum function of mysql will be mentioned to you first. For other related issues, you can continue to pay attention to our industry information if you want to know. Our section content captures some industry news and expertise to share with you every day.

Tags: Functions specialties examples industries statements queries differences expertise topics books content actual data databases news terminology sections formats knowledge articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB OPPO Reno Shulou Technology Shulou Tech Info macOS