日期:2014-05-18  浏览次数:20652 次

sql语句排除两个时间段的问题
select top 10 f_key,f_CreateTime from temp 
where f_key in 
(6345704424639041473,
6345704424875041563,
6345704425098441787,
6345704425148441832,
6345704425214041877)
 --and (f_CreateTime >= '2011-11-16 00:00:00' and f_CreateTime <= '2011-11-16 23:59:59')
and (f_closetime >='2011-11-1' and f_closetime <='2011-11-30')


我怎么写开单时间排除掉一天的数据and (f_CreateTime >= '2011-11-16 00:00:00' and f_CreateTime <= '2011-11-16 23:59:59')
不写子查询 not in (select f_key from temp where f_CreateTime >= '2011-11-16 00:00:00' and f_CreateTime <= '2011-11-16 23:59:59') 还有什么方法,

------解决方案--------------------
是否

SQL code
select top 10 f_key,f_CreateTime from temp  
where f_key in  
(6345704424639041473,
6345704424875041563,
6345704425098441787,
6345704425148441832,
6345704425214041877)
and (f_closetime >='2011-11-1' and f_closetime <='2011-11-30')
and not (f_CreateTime >= '2011-11-16 00:00:00' and f_CreateTime <= '2011-11-16 23:59:59')

------解决方案--------------------
and f_CreateTime not between '2011-11-16 00:00:00' and '2011-11-16 23:59:59'
------解决方案--------------------
或者这样拆开

SQL code
select top 10 f_key,f_CreateTime from temp  
where f_key in  
(6345704424639041473,
6345704424875041563,
6345704425098441787,
6345704425148441832,
6345704425214041877)
and ((f_closetime >='2011-11-1' and f_closetime <'2011-11-16 00:00:00')
or  (f_CreateTime > '2011-11-16 23:59:59' and f_closetime <='2011-11-30')
)

------解决方案--------------------
SQL code
and f_CreateTime <='2011-11-16 00:00:00' and f_CreateTime >='2011-11-16 23:59:59'

------解决方案--------------------
SQL code
(f_CreateTime>='2011-11-1 00:00:00' and f_CreateTime <='2011-11-16 00:00:00') 
or
(f_CreateTime >='2011-11-16 23:59:59' and  f_closetime <='2011-11-30 00:00:00' )