求一条SQL语句,有效马上给分

buqitianxie 2010-04-07 04:24:32
正在做一个小项目,有一表sys_pc,包含字段lastdate(最近开机时间),room(所在机房),还有其它基本信息,不列举。字段类型都为文本。
先要实现如下功能:从数据库中查询出 lastdate小于startdate或者lastdate大于enddate 并且room=1的记录,前两个是或的关系,然后和最后一个机房条件 与,求sql语句!
...全文
100 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
leihuanhuan123 2010-04-07
  • 打赏
  • 举报
回复
select t.lastdate,t.room from sys_pc t where t.lastdate < startdate
or t.lastdate > enddate and t.room=1
平淡面对 2010-04-07
  • 打赏
  • 举报
回复
试这个:
(假设lastdate是“月/日/年”的格式储存的,如果不是这样,就需要先转换成这样的格式)


String startDate = "01/01/2010"; //in month/day/year format
String endDate = "04/01/2010"; //in month/day/year format
String roomNo = "1";

//lastdate must in month/day/year format
String sql = ”Select * from sys_pc "
+ "where ((CDate(lastdate) < ” + "#" + startDate + "# )"
+ " or (CDate(lastdate) > ” + "#" + endDate + "# ))"
+ " and room = '" + roomNo + "'"




buqitianxie 2010-04-07
  • 打赏
  • 举报
回复
回10楼,我要的是不在时间段内
xchh1220 2010-04-07
  • 打赏
  • 举报
回复
lastdate小于startdate或者lastdate大于enddate

这个没反了吗,一般查的都是startdate<=lastdate<=enddate(在时间段内)

lastdate一般来说应该是datetime日期时间(如:2010-04-07 16:45:00),startdate、enddate是日期(如:2010-04-01)

lastdate用文本类型很怪

select * from sys_pc where startdate <= lastdate and lastdate<(enddate+1天) and room=1
wwwtyb 2010-04-07
  • 打赏
  • 举报
回复
前两个是或的关系,然后和最后一个机房条件与
select * from sys_pc where (lastdate <= startdate or lastdate >=enddate) and room=1
eighthgod 2010-04-07
  • 打赏
  • 举报
回复
#- -楼主在搞测试啊..你自己都说得那么明白了.
skran 2010-04-07
  • 打赏
  • 举报
回复
1楼的就可以啊
reui 2010-04-07
  • 打赏
  • 举报
回复
你自己不是很清楚地表述出来了,sql语句也不难了吧
shukuiyan 2010-04-07
  • 打赏
  • 举报
回复
使用union呢?
select * from sys_pc where lastdate<startdate and room=1 UNION
select * from sys_pc where lastdate>enddate and room=1
buqitianxie 2010-04-07
  • 打赏
  • 举报
回复
一楼,用的是access
二楼,就是三个条件,前两个是或得关系,然后和第三个与.
  • 打赏
  • 举报
回复
select * from sys_pc where lastdate <= startdate or lastdate >=enddate and room=1
lp19890601 2010-04-07
  • 打赏
  • 举报
回复
說明白一點
winterAcai 2010-04-07
  • 打赏
  • 举报
回复
oracle 数据库:

select t.lastdate,t.room from sys_pc t where t.lastdate < startdate
or t.lastdate > enddate and t.room=1

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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