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

请一SQL语句,关于多表连接--真难写.
有三个表:
bkind   ,   skind,     other

bkind:
id     cid       cname
1       72         aa
2       35         bb
----------------------
skind

id     bid   bname       sid   sname
1       35     bb             36     mm
2       35     bb             37     mm1
-----------------------
other

id     bid       sid
1       72           -1
2       35           36
3       35           37

怎样查询出
       
1       aa      
2       bb       mm
3       bb       mm1


------解决方案--------------------
select identity(int ,1,1) id,isNull(bkind.cname,skind.bname) name,skind.sname
from bkind full join skind on skind.bname =bkind.cname
------解决方案--------------------

create table bkind
(id int, cid int, cname varchar(4))
insert into bkind select 1, 72, 'aa '
union all select 2, 35, 'bb '

create table skind
(id int, bid int, bname varchar(4),sid int,sname varchar(5))
insert into skind select 1, 35, 'bb ', 36, 'mm '
union all select 2, 35, 'bb ', 37, 'mm1 '

create table other
(id int, bid int, sid int)
insert into other select 1, 72, -1
union all select 2, 35, 36
union all select 3, 35, 37


select a.id,b.cname,c.sname from other a
left join bkind b
on b.cid=a.bid
left join skind c
on c.bname=b.cname and c.sid=a.sid

id cname sname
----------- ----- -----
1 aa NULL
2 bb mm
3 bb mm1

(所影响的行数为 3 行)
------解决方案--------------------
select bkind.cname,skind.sname
from bkind inner join other on bkind.cid=other.bid
left join skind on bkind.cid=skind.bid
------解决方案--------------------
select o.id, b.cname, s.sname
from other as o join bkind as b on o.bid=b.cid
left join skind as s on o.bid=s.bid and o.sid=s.sid

------解决方案--------------------
具体需求?
------解决方案--------------------
好乱
------解决方案--------------------
楼主 需求不明确~
要得到结果不难
但恐怕不是楼主的意思吧?
------解决方案--------------------
other表好像没有用上啊