【发布时间】: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