【问题标题】:Group by with extra features MySQL通过 MySQL 的额外功能分组
【发布时间】:2015-12-19 14:51:35
【问题描述】:

我正在尝试获取所有密钥的总和 对于所有年份(单独),每年都有一个以上,并从三个数据库中获取信息 我可以做到一年,但如何做到这一点。其实还有两年 1996 和 1997

这是 1996 年获得所有物品的方法,与 1997 年获得几乎相同的方法 但是如何加入他们或获得正确的一切

SELECT OrderID, CustomerID, OrderDate, sum(Cost) as TotalCost
FROM (SELECT Orders.CustomerID, Orders.OrderID, Orders.OrderDate, Orders.OrderID, OrderDetails.ProductID, (OrderDetails.Quantity* Products.Price) as Cost
             FROM Orders, OrderDetails, Products
             WHERE Orders.OrderID = OrderDetails.OrderID
             AND OrderDetails.ProductID= Products.ProductID)
WHERE OrderDate<="1996-12-31"
Group by CustomerID

这是我所拥有的表格 订单

+---------+------------+------------+------------+-----------+
| OrderID | CustomerID | EmployeeID | OrderDate  | ShipperID |
+---------+------------+------------+------------+-----------+
|   10248 |         90 |          5 | 1996-07-04 |         3 |
|   10249 |         81 |          6 | 1996-07-05 |         1 |
|   10250 |         34 |          4 | 1996-07-08 |         2 |
|   10251 |         84 |          3 | 1997-07-08 |         1 |
|   10252 |         76 |          4 | 1997-07-09 |         2 |
+---------+------------+------------+------------+-----------+

订单详情

+---------------+---------+-----------+----------+
| OrderDetailID | OrderID | ProductID | Quantity |
+---------------+---------+-----------+----------+
|             1 |   10248 |        1 |       12 |
|             2 |   10248 |        2 |       10 |
|             3 |   10248 |        3 |        5 |
|             4 |   10249 |        1 |        9 |
|             5 |   10249 |        2 |       40 |
|             6 |   10250 |        1 |       10 |
|             7 |   10250 |        2 |       35 |
|             8 |   10250 |        3 |       15 |
+---------------+---------+-----------+----------+

和产品

+-----------+--+------------+------------+--+-------+
| ProductID |  | SupplierID | CategoryID |  | Price |
+-----------+--+------------+------------+--+-------+
|         1 |  |          1 |          1 |  |    18 |
|         2 |  |          1 |          1 |  |    19 |
|         3 |  |          1 |          2 |  |    10 |
+-----------+--+------------+------------+--+-------+

我使用 MySQL 很抱歉发了这么长的帖子

附:它说

没有这样的功能:年份

P.P.S.我曾经使用 SQLlite

【问题讨论】:

  • 表示没有年份功能,可能是OrderDate列的类型不对
  • "No such function: year" - 这个错误信息格式是典型的 sqlite,而不是 mysql。你确定你用的是mysql,而不是sqlite?

标签: mysql database sqlite group-by


【解决方案1】:

您将在聚合中包含year(OrderDate)

SELECT OrderID, CustomerID, year(OrderDate), sum(Cost) as TotalCost
FROM (SELECT Orders.CustomerID, Orders.OrderID, Orders.OrderDate, Orders.OrderID, OrderDetails.ProductID, (OrderDetails.Quantity* Products.Price) as Cost
      FROM Orders JOIN
           OrderDetails
           ON Orders.OrderID = OrderDetails.OrderID JOIN
           Products
           ON OrderDetails.ProductID = Products.ProductID
     ) x
WHERE OrderDate <= '1996-12-31'
Group by CustomerID, year(OrderDate);

其实子查询是不必要的,你应该使用表别名:

SELECT o.CustomerID, year(o.OrderDate), SUM(od.Quantity * p.Price) as Cost
FROM Orders o JOIN
     OrderDetails od
     ON o.OrderID = od.OrderID JOIN
     Products p
     ON od.ProductID = p.ProductID
GROUP BY CustomerId, year(OrderDate)

【讨论】:

  • 1.您应该将 orderid 从外部选择中取出。 2. 为什么需要子查询?
  • 现在您发布的解决方案与我完全相同:)
  • 但是说没有年份功能
  • Year() 函数自 mysql 构思以来就已在 mysql 中可用,我相信。
  • 是的,我相信你,但在这里它不起作用w3schools.com/sql/trysql.asp?filename=trysql_delete
【解决方案2】:

使用 year() mysql 函数从日期中提取年份部分:

select o.customerid, year(o.orderdate) as year, sum(od.Quantity* p.Price) as totalcost
from orders o
inner join orderdetails od on o.orderid=od.orderid
inner join products p on od.productid=p.productid
group by o.customerid, year(o.orderdate)

更新:

根据错误消息,您可能使用的是 sqlite 而不是 mysql。本例使用 substr() 函数获取最左边的 4 个字符,否则逻辑相同:

select o.customerid, substr(o.orderdate,1,4) as year, sum(od.Quantity*  p.Price) as totalcost
from orders o
inner join orderdetails od on o.orderid=od.orderid
inner join products p on od.productid=p.productid
group by o.customerid, substr(o.orderdate,1,4)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    相关资源
    最近更新 更多