日期:2014-05-18  浏览次数:20417 次

多表联合查询的问题 高手请看!!!在线 等 !
SQL   语句如下:
select     isnull(count(x.bit_id),0)   from     base_band   x     join   base_series   y     on   y.bit_bandid=x.bit_id    
join   base_modal   z   on   z.bit_seriesid=y.bit_id
join   veh_vehicle   veh   on   veh.bit_carmodel   =   z.bit_id  
join  
(select   xs.bit_customer_id   from   VEH_SaleOrder   xs  
union   select   jxs.bit_customerid   from   VEH_DealSellInfo   jxs
)   as   hj
on   hj.bit_customer_id=veh.bit_customer_id  

我现在的问题是   其中   最后一个join   的子查询中的数据是应该是整个   查询结果的正确数据,但是   结果会多出   ,怎样让   最后一个join   之前的数据   和   子查询中的数据相吻合?   请高手帮忙!!

------解决方案--------------------
create table #ta
(
[id] int not null
)

create table #tb
(
[id] int not null,
[name] varchar(10)
)

insert into #ta values(1)
insert into #ta values(1)
insert into #ta values(2)
insert into #ta values(2)

insert into #tb values(2, 'a ')
insert into #tb values(2, 'b ')

select #tb.[name] from #ta join #tb on #ta.[id]=#tb.[id]
drop table #ta
drop table #tb

对于#tb来说,它只有两条数据,但联合后的结果是四条,这不也是正常的吗