select a.*,b.inv_weight from (select a.o_id,a.o_datetime,a.o_dept,b.p_name,b.p_size,b.p_id,sum(isnull(b.o_y_weight,b.o_weight)) as o_weight,sum(b.o_y_weight) as o_y_weight,sum(isnull(b.o_y_money_sum,b.o_money_sum)) as money_sum
from p_order as a left join p_orderlist as b on a.o_id=b.o_id  where a.is_print is not null group by a.o_id,a.o_datetime,a.o_dept,b.p_name,b.p_size,b.p_id) as a left join invoice as b
on a.o_id=b.o_id and a.p_id=b.p_id and isnull(a.o_y_weight,a.o_weight) <> b.inv_weight 为什么查不出数据 不加最后一个条件是可以查出数据的 问题出现在最后一个<> 用=也是可以查出数据 谁能帮我解决下

解决方案 »

  1.   

    这个要看你表里的实际数据是什么才能确定,看看内部的子查询
    select a.o_id,a.o_datetime,a.o_dept,b.p_name,b.p_size,b.p_id,
    sum(isnull(b.o_y_weight,b.o_weight)) as o_weight,sum(b.o_y_weight) as o_y_weight,
    sum(isnull(b.o_y_money_sum,b.o_money_sum)) as money_sum
    from p_order as a left join p_orderlist as b on a.o_id=b.o_id 
    where a.is_print is not null group by a.o_id,a.o_datetime,a.o_dept,b.p_name,b.p_size,b.p_id
    查出了什么内容,再判断与外面另一个表连接的时候是否能有符合条件的记录.
      

  2.   

    问题解决了  因为b.inv_weight这列里面有空值  所以用isnull(b.inv_weight,0)就可以查出来了
      

  3.   

    and isnull(isnull(a.o_y_weight,a.o_weight),0) <> isnull(b.inv_weight ,0)
      

  4.   

    COALESCE--也可用COALESCE
    and COALESCE(a.o_y_weight,a.o_weight,0) <> COALESCE(b.inv_weight ,0)
      

  5.   

    select a.*,b.inv_weight from (select a.o_id,a.o_datetime,a.o_dept,b.p_name,b.p_size,b.p_id,sum(isnull(b.o_y_weight,b.o_weight)) as o_weight,sum(b.o_y_weight) as o_y_weight,sum(isnull(b.o_y_money_sum,b.o_money_sum)) as money_sum
    from p_order as a left join p_orderlist as b on a.o_id=b.o_id where a.is_print is not null group by a.o_id,a.o_datetime,a.o_dept,b.p_name,b.p_size,b.p_id) as a left join invoice as b
    on a.o_id=b.o_id and a.p_id=b.p_id and isNull(isnull(a.o_y_weight,a.o_weight),0) <> isdNull(b.inv_weight,0)