有个表test的数据如下,其中有日期字段data date
a 2009-8-1
b 2009-8-2
c 2009-8-3现在要找出2009-8-1到2009-8-2之间的数据,用SQL语句如下:
select * from test where date between '2009-8-1' and '2009-8-2'结果找出来的是所有的记录,而不是2009-8-1到2009-8-2之间的记录,请问这条SQL语句该怎么写?
a 2009-8-1
b 2009-8-2
c 2009-8-3现在要找出2009-8-1到2009-8-2之间的数据,用SQL语句如下:
select * from test where date between '2009-8-1' and '2009-8-2'结果找出来的是所有的记录,而不是2009-8-1到2009-8-2之间的记录,请问这条SQL语句该怎么写?
create table test(data varchar(10), date datetime)
insert into test values('a' , '2009-8-1')
insert into test values('b' , '2009-8-2')
insert into test values('c' , '2009-8-3')
goselect * from test where date between '2009-08-01' and '2009-08-02' select * from test where date between '2009-8-1' and '2009-8-2' drop table test/*
data date
---------- ------------------------------------------------------
a 2009-08-01 00:00:00.000
b 2009-08-02 00:00:00.000(所影响的行数为 2 行)data date
---------- ------------------------------------------------------
a 2009-08-01 00:00:00.000
b 2009-08-02 00:00:00.000(所影响的行数为 2 行)
*/
a 2009-8-1
b 2009-8-2
c 2009-8-3
就算date是字符型
select * from test where date between '2009-8-1' and '2009-8-2'
2009-8-3 也不會查出來吧
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
出现这种情况应该是你表中date字段是字符类型的
查询的时候注意转成Datetime类型的就OK了select * from test where CONVERT(VARCHAR(10),date,120) between '2009-08-01' and '2009-08-02'
沒有永真的條件 如 where 1=1 or(date between '2009-8-1' and '2009-8-2' )
where DATEDIFF(day, date,'2009-8-1')<=0 and DATEDIFF(day,date,'2009-8-2')>=0