where M.Code = A.Code
Union ALL
select M.AutoID,B.Name from MainT M, CodeB B
where M.Code = B.Code
有没有更好的方法?另外如果Type有4种类型呢,又怎么查?
谢谢!
------解决方案--------------------select t.AutoID,A.name,B.name
from MainT t
left join CodeA a on t.code=A.code and t.type=1
left join CodeB b on t.code=B.code and t.type=2
------解决方案----------------------如果type有四种类型,那么继续left join
select t.AutoID,A.name,B.name
from MainT t
left join CodeA a on t.code=A.code and t.type=1
left join CodeB b on t.code=B.code and t.type=2
left join CodeC C on t.code=c.code and t.type=3
left join CodeD D on t.code=d.code and t.type=4
------解决方案--------------------
select T.AutoID,A.Name from MainT T left join CodeA A on T.Code=A.Code where
T.Type=1 union select T.AutoID,B.Name from MainT T left join CodeB B on
T.Code=B.Code where T.Type=2
亲测:
