select distinct a.*
from a,b
where to_char(a.reg_date,'yyyy-mm-dd') >= '2015-08-10'
and to_char(a.reg_date,'yyyy-mm-dd') < '2015-08-11'
and a.card_no = b.card_no
and a.dept_name is not null
and a.dept_name <> '.'
and a.valid_flag = '1'
and (b.in_source = '1' or b.in_source = '2')
and a.reglevl_name is not null
and a.reglevl_name <> '测试挂号级别'
and a.reglevl_name <> '本院'
and a.cancel_opcd is null
and to_char(a.reg_date,'yyyy-mm-dd') >= to_char(b.in_date,'yyyy-mm-dd')
我在select后面增加一列,b.in_date,为什么会将大于a.reg_date的结果筛选出来?
to_char(a.reg_date,'yyyy-mm-dd') < '2015-08-11'
这里应该这样写好点:
a.reg_date< to_date('2015-08-11 00:00:00','yyyy-mm-dd hh24:mi:ss')
and to_char(a.reg_date,'yyyy-mm-dd') >= to_char(b.in_date,'yyyy-mm-dd')这里写成这样
where to_date(date_1,'yyyy-mm-dd') > to_date(date_1,'yyyy-mm-dd');
to_char()是将日期函数转换成字符串,而字符串比较规则是ASCII码,
就是一位一位对比,只要有对应一位大于即大于
如:字符串比较:123和13
很明显第一位都是1,
123第二位是2
13第二位是3
所以字符串比较结果13>123
不应该使用to_char,应该使用to_date
这段代码就整体的可维护性来说,是不易维护的。。。性能也不太好。。。
to_char(a.reg_date,'yyyy-mm-dd') >= to_char(b.in_date,'yyyy-mm-dd') 这是字符串的比较,不是时间的比较
把to_char 改成to_date
to_char(..._date)已经转换为字符类型,字符的比较与日期是不同的。char类型比较是从头开始对应位的比较。