C#里的sql语句如何得到昨天,今天?在线等

Jelena景 2012-09-04 11:23:14
比如我现在有一张表table,然后里面有个创建日期createDate,我现在要统计出createdate在昨天的总数,和今天的总数,如何操作?
...全文
262 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jelena景 2012-09-05
  • 打赏
  • 举报
回复
谢谢各位,或许我没表述清楚,大家貌似都误会我的意思了,不过我已经解决了。
昨天:
string strYes=dateTime.Now.addDay(-1).Toshortdatetime();
dateTime dtYes1=convert.todatetime(strYes+"0:00:00");
dateTime dtYes2=convert.todatetime(strYes+"23:59:59");
其实就这样,我想获得昨天零点到二十三点的时间。谢谢各位啦。
Jelena景 2012-09-05
  • 打赏
  • 举报
回复
我说的不是那个意思亲,不过谢谢你哦[Quote=引用 2 楼 的回复:]

或者用linq 上面那个我也不知道对不对,,
[/Quote]
代码誊写工 2012-09-04
  • 打赏
  • 举报
回复
其实这点儿事儿用SQL自己的时间函数就解决了

select count(*) from table where createdate >= date(now());
select count(*) from table where createdate >= date_add(date(now()), interval -1 day) and createdate < date(now());
天下如山 2012-09-04
  • 打赏
  • 举报
回复

//昨天
select count(1) as yesterday from XX where datediff(d,ProgDate,getdate())=1
//今天
select count(1) as today from XX where datediff(d,ProgDate,getdate())=0
代码誊写工 2012-09-04
  • 打赏
  • 举报
回复
date 今天零点=now.date
date 昨天零点=今天零点.adddays(-1)
select * from table where createdate >= 昨天零点 and createdate < 今天零点
select * from table where createdate >= 今天零点
seominho 2012-09-04
  • 打赏
  • 举报
回复
你是要得到sql语句吗?
stonespace 2012-09-04
  • 打赏
  • 举报
回复
select count(*) from xxx where createDate < @Max and createDate >= @Min

@Max / @Min 参数设置为今天和昨天的时间范围就可以,
zhanglong19891129 2012-09-04
  • 打赏
  • 举报
回复
或者用linq 上面那个我也不知道对不对,,
zhanglong19891129 2012-09-04
  • 打赏
  • 举报
回复
select count(条件) from table;
二当家 2012-09-04
  • 打赏
  • 举报
回复

select convert(varchar(7),ViewDate,120) as month, COUNT(1) as NUM, from PageView
group by convert(varchar(7),ViewDate,120)
order by month desc
我是根据月份来取的 比如 3月10条 4月20条....

但是我始终依稀觉得这样效率应该不高。。。

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧