如何创建 SQL 运算符组合,如 ALL IN,NOT ALL IN

Posted

技术标签:

【中文标题】如何创建 SQL 运算符组合,如 ALL IN,NOT ALL IN【英文标题】:How to create SQL Operators Combination like ALL IN, NOT ALL IN 【发布时间】:2011-05-16 03:18:28 【问题描述】:

我在 Filter 类中有以下 4 个属性。我将解析以下 4 个属性到 StoredProcedure 并获得过滤后的结果。

/// <summary>
/// Gets and sets comma seperated condition ids.
/// Patients must have all these conditions in order to satisfy the filter.
/// </summary>
public string MustHaveAllConditions  get; set; 

/// <summary>
/// Gets and sets comma separated condition ids.
/// Patients must not have all of these conditions in order to satisfy the filter.
/// </summary>
public string MustNotHaveAllConditions  get; set; 

/// <summary>
/// Gets and sets comma separated condition ids.
/// Patients must have at least one of these conditions in order to satisfy the filter.
/// </summary>
public string MustHaveAtLeastOneCondition  get; set; 

/// <summary>
/// Gets and sets comma separated condition ids.
/// Patients must not have at least one of these conditions in order to satisfy the filter.
/// </summary>
public string MustNotHaveAtLeastOneCondition  get; set; 

我的存储过程将有四个参数,如下所示。 例如:

@*MustHaveAll*Conditions = "1,2"

@*MustNotHaveAll*Conditions = "3,4"

@*MustHaveAtLeastOne*Condition = "5,6,7,8"

@*MustNotHaveAtLeastOne*Condition = "9, 10"

我正在使用一个 UDF,它返回一个带有 Ids 列的表。

我的问题:

基本上我可以使用 SQL“IN”运算符来查找具有至少一个 Condition 的患者(即:@MustHaveAtLeastOneCondition)和“NOT IN”运算符组合来过滤 @MustNotHaveAnyConditions。

是否有任何 SQL 运算符(或其他方法)来过滤 MustHaveAllConditions、MustNotHaveAllConditions 参数?

【问题讨论】:

【参考方案1】:
-- Patients
declare @Patient table (PatientID int)

-- Conditions per patient
declare @PatientCondition table (PatientID int, ConditionID int)

-- Conditions table generated from param string
declare @Condition table (ConditionID int)

-- Test data
insert into @Patient
select 1 union all
select 2 union all
select 3

insert into @PatientCondition
select 1, 1 union all
select 1, 2 union all
select 1, 3 union all
select 2, 1 union all
select 3, 3

insert into @Condition
select 1 union all
select 2


-- MustHaveAll
select *
from @Patient as P
where P.PatientID in 
  (
    select PC.PatientID
    from @PatientCondition as PC
      inner join @Condition as C
        on PC.ConditionID = C.ConditionID
    group by PC.PatientID
    having count(PC.ConditionID) = (select count(ConditionID) from @Condition)
  )

--MustNotHaveAll
select *
from @Patient as P
where P.PatientID not in 
  (
    select PC.PatientID
    from @PatientCondition as PC
      inner join @Condition as C
        on PC.ConditionID = C.ConditionID
    group by PC.PatientID
    having count(PC.ConditionID) = (select count(ConditionID) from @Condition)
  )

-- MustHaveAtLeastOne
select *
from @Patient as P
where P.PatientID in
  (
    select PC.PatientID
    from @PatientCondition as PC
      left outer join @Condition as C
        on PC.ConditionID = C.ConditionID
    where C.ConditionID is not null
  )

--MustNotHaveAtLeastOne
select *
from @Patient as P
where P.PatientID not in
  (
    select PC.PatientID
    from @PatientCondition as PC
      left outer join @Condition as C
        on PC.ConditionID = C.ConditionID
    where C.ConditionID is not null
  )

【讨论】:

这太棒了。如果我想在一个 sql 查询中合并这四个条件以获得最终的患者列表,你能告诉我最有效的方法吗? @CharithJ – 您可以将不同查询中的 where 子句添加到一个查询中,并用 and 分隔。 where PatientID in (...) and PatientID in (...) and ...【参考方案2】:

确实没有一种简单的方法可以完成您所描述的操作。我会做的可能是把你的字符串拆分成一个表变量,然后使用右、左和内连接的组合从另一个表变量中删除结果,这是我的输出。

【讨论】:

以上是关于如何创建 SQL 运算符组合,如 ALL IN,NOT ALL IN的主要内容,如果未能解决你的问题,请参考以下文章

mysql过滤数据-------------------------待补充

SQL总结 连表查询

MySQL 运算符

sql语句all的使用

SQL中 ALL的用法

sql中=all和 in的区别