语句一
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年没搞数据库,又忘了。
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年没搞数据库,又忘了。
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
*/
我这儿测试是没问题的
你试试你那儿是不是连接关键字写的有问题