简体   繁体   English

SQL 选择具有特定值的不同 ID

[英]SQL selecting distinct Ids with specific values

Could you tell me please how to find users with FormFieldID ((1 AND 2) OR 3), so the SQL query should return UserIDs: 7, 8, 9.你能告诉我如何找到具有 FormFieldID ((1 AND 2) OR 3) 的用户,所以 SQL 查询应该返回 UserIDs: 7, 8, 9。

The table:桌子:

桌子

I use SQL Server.我使用 SQL Server。

Thank you!谢谢!

I would recommend aggregation and having clause to implement the filtering logc:我会建议聚合和having子句来实现过滤 logc:

select userid
from mytable
group by userid
having 
    (
         max(case when formfieldid = 1 then 1 end) = 1 
         and max(case when formfieldid = 2 then 1 end) = 1
    )
    or max(case when formfieldid = 3 then 1 end) = 1

Depending on your actual database, which you did not tell, there may be neater options to express the conditions.根据您没有告诉的实际数据库,可能有更简洁的选项来表达条件。 For example, in MySQL:例如,在 MySQL 中:

having 
    (max(formfieldid = 1) and max(formfieldid = 2))
    or max(formfieldid = 3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM