我必须找出最近4个月的tot(计数)值,我个人如何才能做到这一点
表结构是
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 ;
这里$ survey = lime_survey_16579 和$ temp = 16579X10X31 这里16579X10X31的值类似于A1 A2 A3 ..
我如何才能提供像
这样的输出tot month
2 july
4 aug
6 sep
9 oct
任何人都可以帮助我吗?
答案 0 :(得分: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
答案 1 :(得分:0)
为此,你可以这样做:
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
您也可以选择将年份放在SELECT行上。
我将month(submitdate)
和group by
子句中的order by
替换为其他表达式。这避免了使用名为隐藏列的MySQL(mis)功能,其中group by
子句中未提及但未在select
子句中汇总的列。其他SQL引擎(以及标准)不允许这样做。