Home > Database > Mysql Tutorial > body text

How to Sum Column Values in MySQL?

Barbara Streisand
Release: 2024-11-22 05:19:15
Original
863 people have browsed it

How to Sum Column Values in MySQL?

Summing Values in Columns with MySQL

In MySQL, adding the values of a column across multiple rows is a common task when analyzing data. This can be accomplished using the SUM() aggregate function.

Problem Description

Consider a table with three columns (A, B, and C):

A B C
2 2 2
4 4 4
6 7 8

The goal is to calculate the sum of values in each column across all rows in the table. The expected result would be:

A B C
12 13 14

MySQL Solution

To sum the values in columns, use the following query:

select sum(A), sum(B), sum(C)
from mytable
where id in (1, 2, 3);
Copy after login

This query uses the SUM() function to calculate the sum of each column's values. The WHERE clause filters the results to include only rows with IDs 1, 2, and 3, ensuring that the sum is calculated across the desired rows.

The above is the detailed content of How to Sum Column Values in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

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 Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template