开发者

Query on Sql Joins

开发者 https://www.devze.com 2022-12-15 21:36 出处:网络
I have three tables. tblLink: (LinkId, LinkName, GroupId, ,SubGroupId) GroupId and SubGroupId are foreign key in tblGroup and tblSubGroup

I have three tables.

tblLink: (LinkId, LinkName, GroupId, ,SubGroupId)

GroupId and SubGroupId are foreign key in tblGroup and tblSubGroup

tblGroup: (GroupId, GroupName)

tblSubGroup: (SubGroupId, SubGroupName)

in tblLink SubGroupId is allowed Null but GroupId is Mandatory.

I want to fetch LinkName, GroupName, SubGroupName for every LinkId in tblLink

I have written a query

SELECT L.LinkName, G.GroupName, SG.SubGroupName FROM tblLink L

Left Join

tblSubGroup SG

开发者_开发技巧 ON

(L.SubGroupId=SG.SubGroupId)

Inner Join

tblGroup G

ON

(L.GroupId=G.GroupId)

If there is no subgroup for some LinkId I want to show NotExist instead of Null


SELECT
    L.LinkName, G.GroupName, 
    ISNULL(SG.SubGroupName, 'NotExist') AS SubGroupName
FROM
    Link L
....
0

精彩评论

暂无评论...
验证码 换一张
取 消