语句一
select a.XX……,sum(b.XX)…… from a left jion (select 一大堆sql,从若干表里取若干数据) b
where a.xx=b.xx语句二
先把 (select 一大堆sql,从若干表里取若干数据) 做成一个view。例如 create view VB as select 一大堆是sql然后再
select a.XX……,sum(b.XX)  …… from a left jion VB b
where a.xx=b.xx这样出来的结果,居然不一样。
这是为什么?以前曾经遇到过,但是N年没搞数据库,又忘了。

解决方案 »

  1.   

    if object_id('tb1')is not null
    drop table tb1
    go
    create table tb1 (id int,value int)
    insert into tb1 
    select  1,1 union all
    select  2,2 union all
    select  3,3 union all
    select  4,4 union all
    select  5,5if object_id('tb2')is not null
    drop table tb2
    go
    create table tb2 (id int,value int)
    insert into tb2 
    select  1,1 union all
    select  2,2 union all
    select  3,3 union all
    select  4,4if object_id('tb3')is not null
    drop table tb3
    go
    create table tb3 (id int,value int)
    insert into tb3 
    select  1,1 union all
    select  2,2 union all
    select  3,3
    --直接查询
    select a.id,sum(a.value) as value from tb1 a  join 
    (select tb2.id,tb2.value from tb2 where not exists(select id,value from tb3 where tb2.id=tb3.id))b
    on a.id=b.id group by a.id
    /*
    -----------
    id  value
    4     4
    */--使用视图实现
    create view view_select
    as
    select tb2.id,tb2.value from tb2 where not exists(select id,value from tb3 where tb2.id=tb3.id)
    go
    select a.id,sum(a.value)as value from tb1 a  join view_select b
    on a.id=b.id group by a.id/*
    -----------
    id  value
    4     4
    */
    我这儿测试是没问题的
    你试试你那儿是不是连接关键字写的有问题
     
      

  2.   

    这个绝对是一样。从sql的解析原理来说,根本没有任何差别。LZ,看下是不是有什么字段漏了或者拼错了。
      

  3.   

    视图里面不允许有带where条件,而“select 一大堆sql”这里面可能有where条件