代码之家  ›  专栏  ›  技术社区  ›  Richard Anthony Hein

返回与where条件部分匹配的列和值的SQL查询

  •  0
  • Richard Anthony Hein  · 技术社区  · 16 年前

    问题是我实际上是在数据表上使用动态LINQAPI,我必须让它生成该查询,或者完全执行其他操作来检查某些表单上的用户定义的验证规则。如果有人对如何处理这个问题有更好的想法,我将不胜感激。

    1 回复  |  直到 16 年前
        1
  •  1
  •   Steve Homer    16 年前

    警告:这很难看,但它可能对你有用。

    
    Select a.*, b.*, mycond1, mycond2, mycond3
    From a
        Inner Join b On a.pk = b.pk
        … rest of normal query …
    
            -- set of conditions --
        Left Outer Join (select 1 as matched where mycondition1) mycond1
        Left Outer Join (select 1 as matched where mycondition2) mycond2
        Left Outer Join (select 1 as matched where mycondition3) mycond3
    
    -- Relationship between conditions
    Where (mycond1.matched is not null or mycond2.matched is not null) and mycond3 is not null
    

    其思想是使用相关子查询返回1或null,这取决于criteria表达式的各个部分对于行是否为true。然后在where子句中应用各个条件表达式之间的逻辑关系。

    如果生成SQL是可行的,那么最好是手动生成,而不是手动维护。