select t.* from tlw_vwratifylog t where t.flowsn='7183800' 
and t.RatiID >= (select min(a.RatiID) from tlw_vwratifylog a where a.StepName='收件' )
可以,但是:
select t.* from tlw_vwratifylog t where t.flowsn='7183800' 
and t.RatiID >= (select max(a.RatiID) from tlw_vwratifylog a where a.StepName='收件' )
就没有数据,why?

解决方案 »

  1.   

    我晕,你下面句子相当于:2
    select t.* from tlw_vwratifylog t where t.flowsn='7183800' and t.RatiID= max(a.RatiID) and StepName='收件' 
    同时三个条件都要满足才能有数据!第一个句子只是满足两个条件:
    select t.* from tlw_vwratifylog t where t.flowsn='7183800' and  StepName='收件' 
      

  2.   

    有表userinfo
        ID USER_NAME USER_REAL_NAME PWD DEPARTMENT_ID COMPANY_ID STATUS LOGIN GROUP_ID 
    1 1 admin 超级系统管理员 ?g?g5b4W4W^ @D%3 1 1 1 1 1 
    2 2 test 测试用 ?g`+c_JLcl+#>G%3 1 1 1 0 1 
    3 3 lijian lijian 7m`+?gMK)%S1S_[# 1 1 1 1 1 
    4 4 aaaa admin :s7m/p@Xclcl--JI 1 1 1 1 1 
    5 6 aaa a b)?g?gF<<Pb;--U( 13 2 1 0 1 
    6 7 bbb weihu 8/0x?g)%2A+#GE0X 15 2 1 0 1 
    7 8 lwf 1  ?g?f?dP\6DH=>G-- 2 1 1 0 1 
    8 9 cxyh 查询帐户 ?g?f?dH?GZH=(84N 2 1 1 0 1 
    9 10 szyh 设置用户 ?g?f?dBVO>?#8?LP 2 1 1 0 1 
    10 11 tjyh 添加用户 ;a?f?dR28}6)[#HY 2 1 1 0 1 我按照你的格式在我的表中写了个结构一样的语句:
    预期目的查出状态为1,并且ID >= 比部门号为1的最大ID,结果应该为0条记录
    select * from userinfo t where t.status='1' and t.id >= (select max(id) from userinfo a where a.department_id= '1')
    运行后结果与预期不一致ID USER_NAME USER_REAL_NAME PWD DEPARTMENT_ID COMPANY_ID STATUS LOGIN GROUP_ID 
    4 aaaa admin :s7m/p@Xclcl--JI 1 1 1 1 1 
    6 aaa a b)?g?gF<<Pb;--U( 13 2 1 0 1 
    7 bbb weihu 8/0x?g)%2A+#GE0X 15 2 1 0 1 
    8 lwf 1 ?g?f?dP\6DH=>G-- 2 1 1 0 1 
    9 cxyh 查询帐户 ?g?f?dH?GZH=(84N 2 1 1 0 1 
    10 szyh 设置用户 ?g?f?dBVO>?#8?LP 2 1 1 0 1 
    11 tjyh 添加用户 ;a?f?dR28}6)[#HY 2 1 1 0 1 
    同样select * from userinfo t where t.status='1' and t.id >= (select min(id) from userinfo a where a.department_id= '1')
    希望结果是4条记录
    实际是 全部记录此语句实际就2个条件 t.status='1' and t,id >=后面查出来的数值
      

  3.   

    查不到结果无非是where中的条件有不满足的,这种包含子查询的语句是可以自己发现问题的
    你可以把你的子查询语句select max(a.RatiID) from tlw_vwratifylog a where a.StepName='收件'
    到底是怎样的值取到,在和前面的主句对照,这样问题就简单了