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

请问: S Q L Server 两表关联查询问题
表data_merch   :

id     merchid   merchmemo
---------------------  
1         a                     b
2         c                     d


表data_bom
id     fmerchid     zmerchid   demo
---------------------------
1       a                         x           demo
2       z                         k           demo


==========
现在要查询在data_merch里面的merchid在data_bom中(fmerchid,zmerchid)出现过的数据

=========
我写的   达不到效果:
select   *   from   data_bom   as   a   left   join   data_merch   as   b     on   a.zmerchid=b.merchid   or   a.fmerchid=b.merchid  

=========


------解决方案--------------------
不太明白楼主的意思 能否写出你希望查出来的数据
------解决方案--------------------
你的查询结果是什么?是data_merch表中符合条件的内容还是???如果我理解得没错的话.你这样写:
select * from data_merch
where merechid in (select fmerchid from data_bom)
or merechid in (select zmerchid from data_bom)

------解决方案--------------------
select *
from
data_bom as a
left join
data_merch as b
on
a.zmerchid=b.merchid
or a.fmerchid=b.merchid
where
b.merchid is not null
and b.merchid is not null