这是我的表格,我想要选定产品的总和:
select sum(`price`) from add_product;
此查询对所有行都运行良好,但我想使用这样的 where 子句:
select sum(`price`) from add_product where product_id=1233 and product_id=1234;
如何做到这一点?
最佳答案
首先, 纠正您的查询,
productID 之间不能有 AND, 使用 OR 代替
SELECT
SUM(`price`)
FROM
add_product
WHERE
product_id=1233 OR product_id=1234;
您还可以使用 IN 子句,
http://www.tutorialspoint.com/mysql/mysql-in-clause.htm
或者,如果您需要按乘积求和,则使用 GROUP BY,
http://www.tutorialspoint.com/mysql/mysql-group-by-clause.htm
SELECT
product_id, SUM(`price`) totprice
FROM
add_product
WHERE
product_id IN (1233,1234)
GROUP BY
product_id;
关于mysql - 如何使用where子句获取mysql中选定行的总和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37266593/