Home >Database >Mysql Tutorial >How to Group and Sum Values in MySQL Using GROUP BY?

How to Group and Sum Values in MySQL Using GROUP BY?

Barbara Streisand
Barbara StreisandOriginal
2025-01-01 08:19:10588browse

How to Group and Sum Values in MySQL Using GROUP BY?

MySQL Query to Group By and Sum Total Values

This query can be used to group and sum values in two columns of a table. In this case, the table has columns word and amount. The query groups the rows by the word column and sums the values in the amount column, providing a result that includes the total amount for each unique word.

The query that achieves this:

SELECT word, SUM(amount)
FROM Data
GROUP BY word

The query attempts to remove the single quote around the word in the GROUP BY clause. The single quote causes the column name to be converted to a string, preventing the correct grouping.

The above is the detailed content of How to Group and Sum Values in MySQL Using GROUP BY?. 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