我有2个表,包含订单,&订单内容(即订单表,& o_contents),&我想从这两个表中获取统计数据,以查看间隔,月,周,年甚至昨天的销售数量!
这里是表格字段:
orders table: ---------------- orderID customerID total pmnt orderDate orderStatus o_contents table ---------------- oContentsID orderID goodsID goodsName quantity price
如何获得过去(即上个月,上周,甚至昨天)特定时期内售出的商品(goodsName)的统计数据
答案 0 :(得分:0)
SELECT ocs.goodsName FROM orders os,o_contents ocs WHERE os.orderID = ocs.orderID AND os.orderDate介于'2011-04-04'和'2011-05-04'之间
答案 1 :(得分:0)
SELECT o_contents.goodsName,
count(orders.orderID)
FROM orders
LEFT JOIN (o_contents) ON (orders.orderID = o_contents.orderID)
GROUP BY MONTH(DATE_SUB(orders.orderDate, INTERVAL 1 MONTH))
这应该会显示上个月已售出的每个好名字的数量,仅作为示例。您可以使用MYSQL日期和时间函数来定义自己的GROUP粒度: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
答案 2 :(得分:0)
SELECT o_contents.goodsName FROM orders LEFT JOIN o_contents
ON orders.orderID = o_contents.orderID
WHERE orders.orderDate >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)