我的语句如下 select A.thetypename, isnull(B.total,0) as inventory
from typeTable  A  
left join
(
select thetypename, 
sum(case when operation = '入货' then goodsnum else - goodsnum end) + 
isnull((select jiandangkucun from typeTable where thetypename=t.thetypename),0)  as total
from ruhuo t 
group by thetypename 
) B
on A.thetypename=B.thetypename  
where A.thetypename like '%轴承%'
我用意是返回 typeTable物品列表中指定物品的库存,但看帮助说where子句在left join里面的作用:“where条件是在临时表生成好后,再对临时表进行过滤的条件”,那么意思是不是说系统一定要把typeTable  所有物品的库存都计算完,然后再筛选出我指定物品的库存? 不能一开始就只是计算我指定物品的库存吗?

解决方案 »

  1.   

    WHERE 执行顺序先于ON先过滤,再JOIN
      

  2.   

    我的语句执行顺序是 where先过滤A表,然后再连接,我现在写的没错?
      

  3.   

    我现在发现我的语句存在以下问题,首先是原始语句:select A.thetypename, isnull(B.total,0) as inventory from typeTable  A  left join(select thetypename, sum(case when operation = '入货' then goodsnum else - goodsnum end) + isnull((select jiandangkucun from typeTable where thetypename=t.thetypename),0)  as total from ruhuo t  group by thetypename ) B on A.thetypename=B.thetypename  where A.thetypename ='轴承'里面的子句:
    select thetypename, sum(case when operation = '入货' then goodsnum else - goodsnum end) + isnull((select jiandangkucun from typeTable where thetypename=t.thetypename),0)  as total from ruhuo t  group by thetypename  如果在ruhuo这个表里面没有相应thetypename的记录,那么即使 select jiandangkucun from typeTable where thetypename=t.thetypename 存在大于0的数值,也是直接返回0,因此不符合我统计的要求,应该如何写才能达到在ruhuo表里即使不存在记录,那么也应该把jiandangkucun这个数字加上,而不是直接返回 0
      

  4.   

    首先是原始语句:select A.thetypename, isnull(B.total,0) as inventory from typeTable  A  left join(select thetypename, sum(case when operation = '入货' then goodsnum else - goodsnum end) + isnull((select jiandangkucun from typeTable where thetypename=t.thetypename),0)  as total from ruhuo t  group by thetypename ) B on A.thetypename=B.thetypename  where A.thetypename ='轴承'
    里面的子句:
    select thetypename, sum(case when operation = '入货' then goodsnum else - goodsnum end) + isnull((select jiandangkucun from typeTable where thetypename=t.thetypename),0)  as total from ruhuo t  group by thetypename  
    如果在ruhuo这个表里面没有相应thetypename的记录,那么即使 select jiandangkucun from typeTable where thetypename=t.thetypename存在大于0的数值,也是直接返回0,因此不符合我统计的要求,应该如何写才能达到在ruhuo表里即使不存在记录,那么也应该把jiandangkucun这个数字加上,而不是直接返回 0
      

  5.   

    SQL server 2000和SQL Server 2005的各个逻辑步骤的简单描述。(8)SELECT (9)DISTINCT  (11)<Top Num> <select list>
    (1)FROM [left_table]
    (3)<join_type> JOIN <right_table>
    (2)        ON <join_condition>
    (4)WHERE <where_condition>
    (5)GROUP BY <group_by_list>
    (6)WITH <CUBE | RollUP>
    (7)HAVING <having_condition>
    (10)ORDER BY <order_by_list>
    逻辑查询处理阶段简介FROM:对FROM子句中的前两个表执行笛卡尔积(Cartesian product)(交叉联接),生成虚拟表VT1 
    ON:对VT1应用ON筛选器。只有那些使<join_condition>为真的行才被插入VT2。 
    OUTER(JOIN):如 果指定了OUTER JOIN(相对于CROSS JOIN 或(INNER JOIN),保留表(preserved table:左外部联接把左表标记为保留表,右外部联接把右表标记为保留表,完全外部联接把两个表都标记为保留表)中未找到匹配的行将作为外部行添加到 VT2,生成VT3.如果FROM子句包含两个以上的表,则对上一个联接生成的结果表和下一个表重复执行步骤1到步骤3,直到处理完所有的表为止。 
    WHERE:对VT3应用WHERE筛选器。只有使<where_condition>为true的行才被插入VT4. 
    GROUP BY:按GROUP BY子句中的列列表对VT4中的行分组,生成VT5. 
    CUBE|ROLLUP:把超组(Suppergroups)插入VT5,生成VT6. 
    HAVING:对VT6应用HAVING筛选器。只有使<having_condition>为true的组才会被插入VT7. 
    SELECT:处理SELECT列表,产生VT8. 
    DISTINCT:将重复的行从VT8中移除,产生VT9. 
    ORDER BY:将VT9中的行按ORDER BY 子句中的列列表排序,生成游标(VC10). 
    TOP:从VC10的开始处选择指定数量或比例的行,生成表VT11,并返回调用者。 
      

  6.   

    WHERE 执行顺序先于ON 先过滤,再JOIN