我怎么能用mysql得到这些? [英] how i can get these with mysql?
问题描述
我是 mysql 的新手.
I am new to mysql.
我有点击调查,期间(日期).现在我必须找出每月的点击次数,例如:
I have on survey with clicks, period(date). Now i have to find out number of clicks per month, like:
MON CLICKS
nov 0
oct 34
sep 67
aug 89
我使用过这样的代码:
select MONTHNAME(period) mon, IFNULL(count(id),0) as Clicks
from survey
where period > DATE_SUB(now(), INTERVAL 3 MONTH)
group by EXTRACT(MONTH FROM period)
它不适用于没有记录.
这里有一件事我想当月没有记录它应该显示为 0:如果 nov
中没有记录,点击次数应该是 0
.
Here one thing I suppose there is no record in that month it should show 0: if there is no record in nov
the number of clicks should be 0
.
my table structure was like this
CREATE TABLE `survey` (
`id` int(2) NOT NULL auto_increment,
`period` datetime default NULL)
过去四个星期我一直在使用
for last four weeks i have used
SELECT uq.timespan, COALESCE(tsq.TotalClicks, 0) as Clicks FROM (
SELECT '22-28 days' as timespan
union SELECT '15-21 days'
union SELECT '8-14 days'
union SELECT 'up to 7 days'
)uq LEFT JOIN (
SELECT CASE
WHEN submitdate >= NOW() - INTERVAL 4 WEEK
AND submitdate < NOW() - INTERVAL 3 WEEK THEN '22-28 days'
WHEN submitdate >= NOW() - INTERVAL 3 WEEK
AND submitdate < NOW() - INTERVAL 2 WEEK THEN '15-21 days'
WHEN submitdate >= NOW() - INTERVAL 2 WEEK
AND submitdate < NOW() - INTERVAL 1 WEEK THEN '8-14 days'
WHEN submitdate >= NOW() - INTERVAL 1 WEEK THEN 'up to 7 days'
END Weeksubmitdate,
count(id) TotalClicks
FROM survey
WHERE submitdate >= NOW() - INTERVAL 4 WEEK
GROUP BY Weeksubmitdate
)tsq ON uq.timespan = tsq.Weeksubmitdate
有什么帮助吗?
推荐答案
我通常会做 Pivot table 来实现这一点.假设您的点击信息存储在名为 SURVEY 的表中,并假设只有点击的日期/时间存储在 SURVEY 表的一列中(这就是您所需要的),那么这里是一种方法:
I usually do Pivot table to achieve this. Assuming your click information is stored into a table named SURVEY and assuming only the date/time of the click is stored into one column of the SURVEY table (which is all what you need) then here is one way to do it:
select year(period),
sum(case when month(period)=1 then 1 else 0 end) jan,
sum(case when month(period)=2 then 1 else 0 end) feb,
sum(case when month(period)=3 then 1 else 0 end) mar,
sum(case when month(period)=4 then 1 else 0 end) apr,
sum(case when month(period)=5 then 1 else 0 end) may,
sum(case when month(period)=6 then 1 else 0 end) jun,
sum(case when month(period)=7 then 1 else 0 end) jul,
sum(case when month(period)=8 then 1 else 0 end) aug,
sum(case when month(period)=9 then 1 else 0 end) sep,
sum(case when month(period)=10 then 1 else 0 end) oct,
sum(case when month(period)=11 then 1 else 0 end) nov,
sum(case when month(period)=11 then 1 else 0 end) dec
from survey
group by year(period)
输出类似于:
---------------------------------------------------------------------------------
| Year | JAN | FEB | MAR | APR | MAY | JUN | JUL | AUG | SEP | OCT | NOV | DEC |
---------------------------------------------------------------------------------
| 2012 | 5 | 20 | 13 | 0 | 0 | 65 | 15 | 0 | 0 | 21 | 0 | 0 |
---------------------------------------------------------------------------------
我什至为你设置了相同的 Fiddle SQLSQL Fiddle 演示
I even set up the same Fiddle SQL for you SQL Fiddle Demo
另一种方式(基于过去 4 个月的列,即使 零 计数):SQL Fiddle 演示
An alternative way (Column based for the last 4 months even with ZERO count): SQL Fiddle Demo
SELECT mon,
sum(clicks) clicks
FROM ( SELECT month(period) mnth,
date_format(period,'%b') mon,
count(1) clicks
FROM survey
WHERE month(period) BETWEEN month(curdate()) - 4 AND month(curdate())
GROUP BY 1, 2
UNION ALL
SELECT 1 mnth, 'Jan' mon, 0 clicks
UNION ALL
SELECT 2 mnth, 'Feb' mon, 0 clicks
UNION ALL
SELECT 3 mnth, 'Mar' mon, 0 clicks
UNION ALL
SELECT 4 mnth, 'Apr' mon, 0 clicks
UNION ALL
SELECT 5 mnth, 'May' mon, 0 clicks
UNION ALL
SELECT 6 mnth, 'Jun' mon, 0 clicks
UNION ALL
SELECT 7 mnth, 'Jul' mon, 0 clicks
UNION ALL
SELECT 8 mnth, 'Aug' mon, 0 clicks
UNION ALL
SELECT 9 mnth, 'Sep' mon, 0 clicks
UNION ALL
SELECT 10 mnth, 'Oct' mon, 0 clicks
UNION ALL
SELECT 11 mnth, 'Nov' mon, 0 clicks
UNION ALL
SELECT 12 mnth, 'Dec' mon, 0 clicks) a
WHERE mnth BETWEEN month(curdate()) - 4 AND month(curdate())
GROUP BY 1
ORDER BY mnth
这篇关于我怎么能用mysql得到这些?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!