我有一个临时表,我插入了值,例如;
SELECT ColourID
INTO #TEMP
FROM [Orders]
WHERE [Order] = 12345
然后,我有一个声明,它做了类似的事情;
SELECT ColourName
FROM Colours
WHERE [ID] IN(SELECT ColourID FROM #TEMP)
DROP TABLE #TEMP
这将返回;
Yellow
Red
我知道在order
上有几个Red实例,而Red的ID在每个临时表中。如何显示每个实例?我尝试过执行COUNT
,但会在每种颜色旁边返回一个。
答案 0 :(得分:3)
IF 我理解正确,您希望为包含红色的每个订单行显示“红色”。然后你需要INNER JOIN
:
SELECT c.ColourName, t.* FROM #TEMP as t INNER JOIN Colours as c ON t.ColourID = c.ID
答案 1 :(得分:1)
如果我理解正确,您需要一个联接来显示临时表中每个实例的非唯一颜色:
SELECT ColourName
FROM Colours, #TEMP
WHERE ID = ColourID
答案 2 :(得分:0)
SELECT DISTINCT ColourName
FROM Colours
WHERE [ID] IN(SELECT ColourID FROM #TEMP)
应该做的诀窍......
答案 3 :(得分:0)
FROM Colours
不是查找表吗?因此每种颜色只定义一次?
这就是你要追求的吗?
SELECT *
FROM [Orders] ords
where exists (select null from #TEMP tem where tem.ColourID = ords.ColourID )