一个简单的SQL 我就是搞不定啊,郁闷 ( 积分: 30 )

  • 主题发起人 主题发起人 cooldren
  • 开始时间 开始时间
C

cooldren

Unregistered / Unconfirmed
GUEST, unregistred user!
一个log表 一个dt字段 类型为datetime
插入数据用的
insert log (dt) values (getdate())

数据显示如下:
2007-02-06 14:34:07.790
2007-02-06 14:37:56.300
2007-02-06 14:39:01.893
2007-02-06 14:40:11.603
2007-02-07 11:32:25.463
2007-02-07 11:42:30.807
2007-02-07 11:44:00.943
2007-02-07 11:45:59.293
2007-02-08 11:46:35.197
2007-02-08 11:51:33.907
2007-02-08 11:52:29.997
2007-02-08 12:06:27.470
2007-02-09 12:07:19.397

查某天到某天的SQL语句怎么写?
select * from log where dt between '2007-02-07'and '07-02-09'
select * from log where dt>='2007-02-06' and dt<='07-02-09'
都有一天查询不到?
 
后面的07改为2007,

或者select * from log where dt between 2007-02-06 14:37:56'and '2007-02-08 11:52:29'试试

没有数据库你自己试吧
 
select * from log where dt between '2007-02-07'and '2007-02-09'
select * from log where dt>='2007-02-06' and dt<='2007-02-09'
改成
select * from log where dt between '2007-02-07'and '2007-02-09 12:59:59:999'
select * from log where dt>='2007-02-06' and dt<='2007-02-09 12:59:59:999'
试试
 
select * from log where dt between '2007-02-07'and '2007-02-09 23:59:59:999'
select * from log where dt>='2007-02-06' and dt<='2007-02-09 23:59:59:999'
 
你只写了日期, 那后面的时间部分, 被默认为是 00:00:00.000
所以......select * from log where dt>='2007-02-06' and dt<='07-02-09'
只能查出 6, 7, 8 三个日期的数据.
想要再包括9日的数据应该把最后那个日期写成 2007-02-10 才行
select * from log where dt>='2007-02-06' and dt<='2007-02-10'
这是从6日 0点 开始 到10日0点结止
 
select * from log where dt between '2007-02-07'and '2007-02-09 12:59:59:999'
select * from log where dt>='2007-02-06' and dt<='2007-02-09 12:59:59:999'
都可以,谢谢了
 
多人接受答案了。
 

Similar threads

后退
顶部