如何检查一对多表是否在临时表中包含一个或多个结果?
@colors Temp Table
------
Red
Blue
Green
Pink
Yellow
SELECT
u.userID,
u.color
FROM
USERS as u
WHERE
u.field = '123'
AND
(
(SELECT t2.userColor FROM TABLE2 as t2 WHERE t1.userID = u.userID) IN (SELECT color FROM @colors)
)
在这种情况下,TABLE2包含一个或多个结果,例如用户的“RED”和“GREEN”。我需要能够看到临时表中是否存在其中任何一个。
简而言之,(SELECT t2.userColor FROM TABLE2 as t2 WHERE t1.userID = u.userID)
可以包含一个或多个用户记录,我需要查看临时表中是否存在这些颜色。
答案 0 :(得分:1)
我认为最简单的方法是使用=max(if(d3:l3="major", e3:m3))
,因为我无法获得返回任何匹配颜色信息所需的印象。
exists
答案 1 :(得分:0)
简单使用cross / outer apply:
SELECT
u.userID,
u.color,
[tmpColors].[count]
FROM
USERS as u
outer apply
(select count(1) as [count] from Table2 t join @colors c on c.color = t.userColor where t.userId = u.userId) as [tmpColors]
WHERE
u.field = '123'