这是我的表,我想选择产品的总和:如何在mysql中使用where子句获取选定行的总和?
select sum(`price`) from add_product;
此查询工作正常进行的所有行,但我想用where子句是这样的:
select sum(`price`) from add_product where product_id=1233 and product_id=1234;
如何做到这一点?
这是我的表,我想选择产品的总和:如何在mysql中使用where子句获取选定行的总和?
select sum(`price`) from add_product;
此查询工作正常进行的所有行,但我想用where子句是这样的:
select sum(`price`) from add_product where product_id=1233 and product_id=1234;
如何做到这一点?
首先 纠正你的查询,
你不能和你productIDs之间, 使用或者替代
SELECT
SUM(`price`)
FROM
add_product
WHERE
product_id=1233 OR product_id=1234;
你也可以使用IN子句,
http://www.tutorialspoint.com/mysql/mysql-in-clause.htm或者,如果您需要product-wise SUM,则使用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;
而不是使用OR的,您可以使用多个值。
select sum(`price`) from add_product where product_id in (1233,1234);
使用BETWEEN
和AND
。
对于2 'product_id'
:
SELECT SUM('price') FROM 'add_product' WHERE 'product_id' BETWEEN 1233 AND 1234
对于3 'product_id'
以升序排列:
SELECT SUM('price') FROM 'add_product' WHERE 'product_id' BETWEEN 1233 AND 1235 ORDER BY 'product_id' ASC
什么是你正面临着与查询问题? – Wistar
我认为你需要从add_product中选择sum(price),其中product_id在(1233,1234);' –