我正在尝试连接两个表,一个是标签表,另一个是桥接tagID和slotID的表。我想创建一个返回标签列表的sproc,以及一列使用这些类别的插槽。
tagDetails
tagID tagDescription
1 red
2 blue
3 green
4 purple
tagBridge
tagID slotID
1 1
1 3
2 5
3 1
4 1
如果它的slotID参数为1,我希望sproc返回这样的结果
tagID tagDescription slotID
1 red yes
2 blue no
3 green yes
4 purple yes
我做了这样的蠢货,但我知道它不能真正正常工作
SELECT tagDetails.tagID, tagDetails.tagDescription, tagBridge.slotID
FROM tagDetails
LEFT JOIN tagBridge
ON tagDetails.tagID = tagBridge.tagID
where tagBridge.slotID = 1
任何想法?
答案 0 :(得分:2)
使用:
SELECT td.tagid,
td.tagdescription,
CASE
WHEN tb.slotid = 1 THEN 'Yes'
ELSE 'No'
END AS slotid
FROM TAGDETAILS td
LEFT JOIN TAGBRIDGE tb ON tb.tagid = td.tagid
答案 1 :(得分:1)
易:
Select tagID, tagDescription,
Case When Exists
(Select * From tagBridge
Where tagId = t.TagId
And slotId = @SlotId)
Then 'yes' else 'no' End SlotId
From tagdetails t