如何使用MySQL和SUM函数从我的数据库中获取总分?

时间:2015-11-30 11:14:06

标签: mysql xampp

请参阅下面我创建的用于从我的数据库生成数据的代码:

SELECT customerorderdetails.OrderDate,Discount,Quantity, products.ProductName,
       ProductCost,Points FROM customerorderdetails 
INNER JOIN products ON  products.ProductID=customerorderdetails.ProductID
 OrderDate  Discount    Quantity    ProductName     ProductCost     Points  
2015-09-01  10              2        Milk            0.50             5
2015-09-01  10              2         Mild Cheese    2.00             6
2015-09-01  10              1        Edam Cheese     2.10             5
2015-09-01  10              1        Eggs            1.00             3
2015-09-01  10              2        Butter          1.00             2
2015-09-01  10              2        Eggs             1.00            3

我现在需要创建一个mysql查询来将所有的点添加到一起。 我试过以下代码没有影响。

SELECT SUM customerorderdetails.OrderDate,Discount,Quantity, 
products.ProductName,ProductCost,Points, 
FROM customerorderdetails
INNER JOIN products
ON products.ProductID=customerorderdetails.ProductID
WHERE customerorderdetails.ProductName=''
AND customerorderdetails.OrderDate BETWEEN '2015-09-01' AND '2015-9-04'
AND products.ProductName=''

然后我将代码更改为以下内容,但现在得到错误1064,

SELECT customerorderdetails.OrderDate,Discount,Quantity,
 products.ProductName,ProductCost,Points,
FROM customerorderdetails
INNER JOIN products
ON products.ProductID=customerorderdetails.ProductID
AND customerorderdetails.OrderDate BETWEEN '2015-09-01' AND '2015-9-04'
SELECT SUM(Points) FROM products

1 个答案:

答案 0 :(得分:1)

试试这个

SELECT  customerorderdetails.OrderDate,SUM(Discount) AS Discount ,SUM(Quantity) AS Quantity, 
products.ProductName,SUM(ProductCost) AS ProductCost,SUM(Points)  AS Points, 
FROM customerorderdetails
INNER JOIN products
ON products.ProductID=customerorderdetails.ProductID
WHERE customerorderdetails.OrderDate BETWEEN '2015-09-01' AND '2015-9-04'
GROUP BY customerorderdetails.ProductID