现在需要一条sql 查询当天前一天0点到24点期间 a字段的数量
解决方案 »
- 页面无刷新并自动去获取数据库中的数据
- hibernate查询的数据库表如何用JSTL表示?
- SCWCD、SCMAD高分通过,转让考试资料
- 如何在一个struts项目中,实现图片动态的显示
- 刚学java,用的是struts1.0,1.0里面怎么什么都是手写的呀,各位看看这是什么错误,怎么改呀
- 如何制作互动的下拉列表(两个列表都涉及到数据库),在线等待!!!!
- 怎样从orcale中取出音乐类型数据,字名=showmusic 类型=(blob)??????
- 急!!!请教各位大侠jsp中的"/"路径标识符的问题
- 关于jsp的配置,急急急急!!!!!!!
- 请问下c3p0的connection为什么每次都是新的呢?
- 求教struts问题
- 关于后台添加了内容 在前台显示问题?大家帮帮忙
from table
where convert(char(10),日期列,111) = '2009/09/25'
oracle的
where [columnName] = 'a'
and [表中记录时间的那个字段] >= convert(varchar(10),dateadd(day,-1,getdate()) ,120)+' 00:00:00'
and [表中记录时间的那个字段] <= convert(varchar(10),dateadd(day,-1,getdate()) ,120)+' 23:59:59'
SELECT count(*) allpages FROM lybbs_posttopic where postattime between CONVERT(datetime,CONVERT(varchar,GetDate()-1,120)+' '+'00:00:00') and CONVERT(datetime,CONVERT(varchar,GetDate()-1,120)+' '+'23:59:59') ;
mysql中 postattime 记录时间的字段
120格式化之后是'2009-09-25'哪位仁兄中的日期列是你表里面时间的字段,'2009/09/25'你换成当成日期的前一天,再转换一下,用dateadd,看我的回复
select count(*) from 表名 where day(表中记录时间的那个字段)=day(curdate())-1这个你试试吧
String str2=new Integer(postattime.substring(5,6)).tostring();
String str3=new Integer(postattime.substring(8,9)).tostring();select count(*) 列名 from 表名 where year((getdate()-1))='"+str1+"' and month((getdate()-1))='"+str2+"' and day((getdate()-1))='"+str3"'
select count(*) 列名 from 表名 where year(postattime)= year((getdate()-1 and month(postattime)=month((getdate()-1)) and day(postattime)=day((getdate()-1))
我再说说需求吧
现在需要一条sql 查询当天前一天0点到24点期间 a字段的数量 我的数据库是mysql数据库postattime这个字段是存时间的字段 类型是datetime 那位大侠帮帮忙哦
select count(*) 列名 from 表名 where year(postattime)= year((getdate()-1)) and month(postattime)=month((getdate()-1)) and day(postattime)=day((getdate()-1))
sqlserver2005验证过了!
这样写可以解决
select * from lybbs_posttopic where year(postattime)= year((now()-1)) and month(postattime)=month((now()-1)) and day(postattime)=day((now()-1))
和
select now()-1 from lybbs_posttopic
你试下打印出来有什么区别
和
select now()-1 from lybbs_posttopic
两个打印出来都一样?
问题1:select now() from lybbs_posttopic 打印出来是什么?
问题2:select now()-1 from lybbs_posttopic 打印出来是什么?
SELECT count(*) FROM 表名 WHERE DATEDIFF(day,时间字段,NOW())=1
这个可以解决,代码也少些
mysql> SELECT DATEDIFF(NOW(),'2009-09-24');
+------------------------------+
| DATEDIFF(NOW(),'2009-09-24') |
+------------------------------+
| 1 |
+------------------------------+
1 row in set (0.14 sec)mysql>
convert,datediff,再运算的时候都会需要时间的,尤其是表里很多记录的时候,
所以,你可以在程序里面定义一个存储前一天的起始时间和一个结束时间,然后,拼成一个sql
比如你最后算完的一个startDate,一个是endDate那么拼成sqlselect count(*) aCount from [table]
where [columnName] = 'a'
and [表中记录时间的那个字段] >= startDate
and [表中记录时间的那个字段] <= dateDate
如果你时间设置了聚集索引,直搜索范围内的记录,这样效率会高些而运算要把每条记录都进行一次运算
from table
where postattime >= CURDATE()
and postattime < DATE_ADD(CURDATE(), INTERVAL 1 DAY);