Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用where子句获取mysql中选定行的总和?_Mysql_Sql_Select_Sum - Fatal编程技术网

如何使用where子句获取mysql中选定行的总和?

如何使用where子句获取mysql中选定行的总和?,mysql,sql,select,sum,Mysql,Sql,Select,Sum,这是我的表,我想要一个选定产品的总和: 此查询对所有行都很有效,但我希望使用where子句,如下所示: select sum(`price`) from add_product where product_id=1233 and product_id=1234; 如何做到这一点?首先, 纠正你的疑问 在ProductID之间不能有和, 使用或替代 SELECT SUM(`price`) FROM add_product

这是我的表,我想要一个选定产品的总和:

此查询对所有行都很有效,但我希望使用where子句,如下所示:

select sum(`price`) from add_product where product_id=1233 and product_id=1234;
如何做到这一点?

首先, 纠正你的疑问

在ProductID之间不能有和, 使用或替代

    SELECT 
            SUM(`price`) 
    FROM 
            add_product 
    WHERE
            product_id=1233 OR product_id=1234; 
您也可以在第条中使用

或者,如果您需要乘积形式的总和,则使用GROUP BY


您可以使用in来表示多个值,而不是使用OR

select sum(`price`) from add_product where product_id in (1233,1234);

在和之间使用

对于2“产品标识”:

SELECT SUM('price') FROM 'add_product' WHERE 'product_id' BETWEEN 1233 AND 1234
对于3个升序的“产品id”:

SELECT SUM('price') FROM 'add_product' WHERE 'product_id' BETWEEN 1233 AND 1235 ORDER BY 'product_id' ASC

您在该查询中面临的问题是什么?我认为您需要
从add_product where product_id in(1233,1234)中选择sum(price)
SELECT SUM('price') FROM 'add_product' WHERE 'product_id' BETWEEN 1233 AND 1234
SELECT SUM('price') FROM 'add_product' WHERE 'product_id' BETWEEN 1233 AND 1235 ORDER BY 'product_id' ASC