表中的字段有 姓名 name 地址address 创建时间time  状态status ,状态在表中分1,2,3,4  四种我写了个对表的查询语句 select * from table where status = '1' or status = '' or status = '3' or status = '' and name like '%s%' and address like '%%' and time >= to_date('2011-09-01','yyyy/mm/dd') and time <= to_date('2011-11-02','yyyy/mm/dd')我想查状态为1,3 时间段在2011-09-01 - 2011-11-02 名字中带s的字段
为什么只把1,3状态的字段查出来了

解决方案 »

  1.   

    楼主对问题的描述不够清晰,“为什么只把1,3状态的字段查出来了”这句话很难理解。
    楼主的SQL中有两处明显的错误:
    1. status = '' 这样是查不出来status为空的记录的,需要改写为 status is null
    2. address like '%%' 这个条件会过滤掉address为空的记录,如果楼主的需求就是要过滤掉address为空的记录,建议楼主显示使用address is not null为好。
      

  2.   

    括号很重要,这才是lz想要的吧 
    select * from table where (status = '1' or status = '3') and name like '%s%' and time >= to_date('2011-09-01','yyyy/mm/dd') and time <= to_date('2011-11-02','yyyy/mm/dd')
      

  3.   

    如果想查截至 2011-11-02的数据,日期有到时分秒的,应该用:
    to_date('2011-11-03','yyyy/mm/dd')-1/86400
    即:当天的最后一秒