[英]How to extract records by comparing two different values in one column
我需要检查一列中的2个或更多值并提取数据。 它不像使用“ in”运算符。
示例:我有一个带有两列用户ID,标准ID的表。 我的应用程序发送标准ID的多个值。 现在,我需要找到有权使用所有标准的用户。
在下面的示例中,我需要找到映射到529和551的“ UserID”。
UserID StandardId
9- 529
12- 529
12- 551
15- 529
17- 551
17- 529
18- 529
21- 529
21- 551
49- 529
58- 529
60- 529
62- 529
62- 551
64- 529
64- 551
81- 529
83- 529
83- 551
226- 551
226- 529
298- 551
298- 529
335- 529
根据当前的映射,有7个标准需要验证。 这种映射将来可能会增长。
我可能无法完全理解您的问题,但这行得通吗?
SELECT UserId FROM [table] WHERE StandardId = 529
INTERSECT
SELECT UserId FROM [table] WHERE StandardId = 551
您可以在此处阅读有关INTERSECT
查询的更多信息。
我认为您可以对ID进行分组并创建汇总函数以检查其是否与这两个值匹配
尝试这个
Select t.id
from table t
join table t1
on t.userid = t1.userid
where t.standardid= 529 and t1.standardid = 551
SELECT UserId FROM [table] WHERE StandardId IN (529, 551/*you can pass in the list here*/)
GROUP BY UserId
HAVING COUNT(StandardId) > 1 /*can change to more when the list is longer*/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.