我正在努力在给定的时间跨度内显示每个月的订单数量以及其他一些限制因素。这是我的询问。
array_size d[q_length];
我正在寻找这样的结果:
SELECT month(o.ord_date) as month, COUNT(o.ord_id) as January, COUNT(o.ord_id) as February, COUNT(o.ord_id) as March, COUNT(o.ord_id) as April,
COUNT(o.ord_id) as May, COUNT(o.ord_id) as June, COUNT(o.ord_id) as July, COUNT(o.ord_id) as August, COUNT(o.ord_id) as September,
COUNT(o.ord_id) as October, COUNT(o.ord_id) as November, COUNT(o.ord_id) as December
FROM hotels h, countries r, cities c, orders o LEFT JOIN trips t ON o.trp_id=t.trp_id
WHERE o.ord_date>'2016-01-01' AND o.ord_date<'2017-01-05' AND t.spr_id IN ('34','68','53')
AND o.htl_id=h.htl_id AND h.ctr_id = r.ctr_id AND h.cty_id = c.cty_id AND r.ctr_id = 245
GROUP BY month(o.ord_date)
ORDER BY month ASC
提前致谢。
编辑: 这是订单表定义P.Salmon请求:
| Jan | Feb | March | Apr | May | Jun | Jul | Aug | Sept | Oct | Nov | Dec
Jan | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1
Feb | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1
March | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1
Apr | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1
May | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1
Jun | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1
Jul | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1
Aug | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1
Sept | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1
Oct | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1
Nov | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1
Dec | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1
这里有几个需要解释的字段:
CREATE TABLE IF NOT EXISTS `orders` (
`ord_id` int(11) NOT NULL AUTO_INCREMENT,
`ord_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`ord_deliv_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`trp_id` int(11) NOT NULL DEFAULT '0',
`htl_id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`ord_id`)
) ENGINE=MyISAM AUTO_INCREMENT=35026 DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
订购时的日期ord_date
日期应该交付订单。在这种情况下,行程(参见ord_deliv_date
)的日期将开始。trp_id
是对酒店表的引用,其中存储了htl_id
和ctr_id
。答案 0 :(得分:0)
条件聚合可能就是您所需要的。 如果我们忽略了连接和条件,那么
select month(ord_date),
sum(case when month(ord_deliv_date) = 1 then 1 else 0 end) as Jan,
sum(case when month(ord_deliv_date) = 2 then 1 else 0 end) as Feb,
.
.
sum(case when month(ord_deliv_date) = 12 then 1 else 0 end) as Dec
from orders
group by month(ord_date)