我想在数据库的表中查询当前时间是否在表中的的开始时间和结束时间之间,不知查询语句怎么写,请指教。。。。

dawei424 2008-04-08 05:02:34
我想在数据库的表中查询当前时间是否在表中的的开始时间和结束时间之间,不知查询语句怎么写,请指教。。。。
...全文
329 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
lfy_xj 2008-04-11
  • 打赏
  • 举报
回复
m_timefrom m_timeto是COleDateTime类型
lfy_xj 2008-04-11
  • 打赏
  • 举报
回复
strtemp1=m_timefrom.Format("%Y-%m-%d");
strtemp2=m_timeto.Format("%Y-%m-%d");

sql.Format("SELECT * FROM table WHERE table.日期 BETWEEN #%s# AND #%s#",strtemp1,strtemp2);
我的这样 通过了的
wmouse 2008-04-10
  • 打赏
  • 举报
回复
你用二楼的方法直接用数据库自己的getdate() 不就好了么?
tianloveyou 2008-04-10
  • 打赏
  • 举报
回复
如果时间格式为年月日,则
CTime cur=CTime::GetCurrent();
CString strSql.Format("select * from table where startTime<='%s' and endTime>= '%s'",cur.Format("%Y-%m-%d"),cur.Format("%Y-%m-%d"))
如果不是.就参照5楼的那样做.其实只是给你一个参照本.而不是一个答案.真正答案要靠自己实现的了
mr.zhoux 2008-04-09
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 dawei424 的回复:]
我试过了,照5楼这样根据字符串来查询是不准确的,只有根据datetime来查询,但不知道'%s' 用什么来表示datetime变量。
[/Quote]

只是给你个方法,具体的逻辑还是你来定啊..你到底要查什么自己都不知道么?
dawei424 2008-04-09
  • 打赏
  • 举报
回复
我试过了,照5楼这样根据字符串来查询是不准确的,只有根据datetime来查询,但不知道'%s' 用什么来表示datetime变量。
内存泄漏 2008-04-09
  • 打赏
  • 举报
回复
是什么数据库??
mr.zhoux 2008-04-09
  • 打赏
  • 举报
回复
strSQL.Format(_T("select the_time from c_timer where startTime <= '%s' and endTime >= '%s' "), currentTime.Format("%y-%m-%d %H:%M:%S"),currentTime.Format("%y-%m-%d %H:%M:%S"));
dawei424 2008-04-09
  • 打赏
  • 举报
回复
上面写的有错,这样写:COleDateTime currentTime = COleDateTime::GetCurrentTime();
strSQL.Format(_T("select the_time from c_timer where startTime <= '%s' and endTime >= '%s' "), currentTime,currentTime );
dawei424 2008-04-09
  • 打赏
  • 举报
回复
我的表里每条记录都有开始1楼所说的startTime和endTime,我现在是要查询当前时间在哪些记录的startTime和endTime之间,COleDateTime currentTime = COleDateTime::GetCurrentTime();
strSQL.Format(_T("select the_time from c_timer where startTime <= '%s' and endTime >= '%s' "), currentTime ); 这样写查询语句请帮看看。
  • 打赏
  • 举报
回复
SELECT * FROM YourTable WHERE getdate() BETWEEN startTime AND endTime
zaodt 2008-04-08
  • 打赏
  • 举报
回复

比如表中的“开始时间”和“结束时间”字段名为:

startTime

endTime


SELECT * FROM YourTable WHERE '2008.4.8 19:00:00' BETWEEN startTime AND endTime



在 VC++ 中,只要生成上边的 SQL 语句即可。

4,017

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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