2012-10-22 181 views
0

我必須找出TOT(計數)與幾個月值過去4個月單獨我怎麼能做到這一點最近四個月的總和(count())?

表stucture被

CREATE TABLE `lime_survey_16579` ( 
`id` int(11) NOT NULL auto_increment, 
`submitdate` datetime default NULL, 
`lastpage` int(11) default NULL, 
`startlanguage` varchar(20) collate utf8_unicode_ci NOT NULL, 
`token` varchar(36) collate utf8_unicode_ci default NULL, 
`16579X10X31` varchar(5) collate utf8_unicode_ci default NULLPRIMARY KEY (`id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=480 ; 

這裏$調查= lime_survey_16579 和$ TEMP = 16579X10X31 這裏16579X10X31有像A1 A2 A3值..

我怎樣才能做到這一點給輸出像

tot month 

2 july 
4 aug 
6 sep 
9 oct 

任何人都可以幫助我嗎?

回答

1

請嘗試以下所有數據:

SELECT count(id) as tot, MONTHNAME(submitdate) as `month` 
FROM lime_survey_16579 
GROUP BY month(submitdate) 
ORDER BY month(submitdate) 

要限制數據持續4月初,請嘗試以下:

SELECT count(id) as tot, MONTHNAME(submitdate) as `month` 
FROM lime_survey_16579 
GROUP BY month(submitdate) 
ORDER BY month(submitdate) DESC 
LIMIT 4 
0

SEL的解決方案是不會,如果你有數據去上班進入前一年。據推測,你只需要最近的「jul」記錄,而不是所有這些記錄的總和。

對於這一點,你可以這樣做:

SELECT count(id) as tot, MONTHNAME(submitdate) as `month` 
FROM lime_survey_16579 
GROUP BY monthname(submitdate), year(submitdate) 
ORDER BY min(submitdate) DESC 
LIMIT 4 

你可以任意把今年選擇線爲好。

我將group byorder by中的month(submitdate)替換爲其他表達式。這避免了使用名爲的隱藏列的MySQL(mis)功能,其中在group by子句中沒有提及且未彙總的列在select子句中是允許的。其他SQL引擎(以及標準)不允許這樣做。