Today, we will be discussing about the SUM function. Instead of solving for the sum of a certain column using PHP or other programming language, you can achieve it by using the SQL SUM function in your query.
Let's say you have the following data on your SALES table and you will solve for the total sales.
Query SUM Function
select SUM(total) from sales;
Output SUM Function
SUM(total) is not quite a straight forward name for the total sales. You can used an alias to organize your output easily by using the keyword as.
Query using alias
select SUM(total) as TotalSales from sales
The TotalSales on the above query there is you alias for the sum of all total. Or you can remove the as word and the output will still be the same.
Output
Hope this helps! Feel free to post a comment below for any questions. Thank you! ^_^
No comments:
Post a Comment