這是我的表,我想選擇產品的總和:如何在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);' –