要对具有相同ID的行求和,请使用GROUP BY HAVING子句。
让我们创建一个表-
mysql> create table demo84 -> ( -> id int, -> price int -> ) -> ;
以下是对具有相同ID的行求和的查询-
mysql> select id,sum(price) as Total from demo84 -> group by id -> having sum(demo84.price) >=2000;
这将产生以下输出-
输出结果
+------+-------+ | id | Total | +------+-------+ | 1 | 4000 | | 2 | 4000 | +------+-------+ 2 rows in set (0.00 sec)