数据库结构:
id hit times
1 1 2008年2月19日
2 26 2008年2月27日
3 32 2008年2月27日select sum(hit)as wk,times from hit where id=1005 group by times
得到结果:
1 2009年12月10日
2 2009年12月16日
1 2009年12月17日
1 2009年12月19日
983 2009年12月1日
1262 2009年12月2日
1201 2009年12月3日
857 2009年12月4日
951 2009年12月5日
924 2009年12月6日
312 2009年12月7日
4 2009年12月8日
2 2009年12月9日
select sum(hit)as wk,times from hit where id=1005 and replace(replace(replace(times,'年','-'),'月','-'),'日','')>=replace(replace(replace('2009年12月20日','年','-'),'月','-'),'日','') group by times
得到结果:
1201 2009年12月3日
857 2009年12月4日
951 2009年12月5日
924 2009年12月6日
312 2009年12月7日
4 2009年12月8日
2 2009年12月9日奇怪为何最后的语句会得到这样的结果?求高人指点。。
id hit times
1 1 2008年2月19日
2 26 2008年2月27日
3 32 2008年2月27日select sum(hit)as wk,times from hit where id=1005 group by times
得到结果:
1 2009年12月10日
2 2009年12月16日
1 2009年12月17日
1 2009年12月19日
983 2009年12月1日
1262 2009年12月2日
1201 2009年12月3日
857 2009年12月4日
951 2009年12月5日
924 2009年12月6日
312 2009年12月7日
4 2009年12月8日
2 2009年12月9日
select sum(hit)as wk,times from hit where id=1005 and replace(replace(replace(times,'年','-'),'月','-'),'日','')>=replace(replace(replace('2009年12月20日','年','-'),'月','-'),'日','') group by times
得到结果:
1201 2009年12月3日
857 2009年12月4日
951 2009年12月5日
924 2009年12月6日
312 2009年12月7日
4 2009年12月8日
2 2009年12月9日奇怪为何最后的语句会得到这样的结果?求高人指点。。
from hit where id=1005 and cast(replace(replace(replace(times ,'年' , '-'),'月','-'),'日','') <= '2009-12-20'
group by cast(replace(replace(replace(times ,'年' , '-'),'月','-'),'日','')
或者格式化一下就不会有问题2009年12月3日
格式化成
2009年12月03日2009年9月3日
格式化成
2009年09月03日
and cast(replace(replace(replace(times,'年','-'),'月','-'),'日','') as datetime)
>=cast(replace(replace(replace('2009年12月20日','年','-'),'月','-'),'日','') as datetime)
from hit
where id=1005 and cast(replace(replace(replace(times ,'年' , '-'),'月','-'),'日','') as datetime) <= '2009-12-20'
group by cast(replace(replace(replace(times ,'年' , '-'),'月','-'),'日','') as datetime)
sum(hit)as wk ,
cast(replace(replace(replace(times ,'年' , '-'),'月','-'),'日','') as datetime)
from
hit where id=1005
and
cast(replace(replace(replace(times ,'年' , '-'),'月','-'),'日','') <= '2009-12-20'
group by
cast(replace(replace(replace(times ,'年' , '-'),'月','-'),'日','')
group by times
之前用ACCESS的,在ACCESS下直接比较就可以的,好像在ACCESS是可以自己转换的。做这是很小的东西,主要是自己用的,效率不用考虑,只是写的时候感觉用什么类型方便就用什么了。
加个GROUP BY 就OK了,谢谢,给分