Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何在表联接中组合多个条件?_Sql_Sql Server - Fatal编程技术网

Sql 如何在表联接中组合多个条件?

Sql 如何在表联接中组合多个条件?,sql,sql-server,Sql,Sql Server,简单地说,我有两个规则表,一个列出所有规则,另一个列出规则的详细信息: Rule_ID Rule_Name 1 "Rule Name 1" 2 "Rule Name 2" Target_Rule_ID Condition 1 >10 1 <20 1 !=15 1 !=18 2 >30 并得出如下结果: ID Value

简单地说,我有两个规则表,一个列出所有规则,另一个列出规则的详细信息:

Rule_ID Rule_Name 
1      "Rule Name 1"
2      "Rule Name 2"

Target_Rule_ID Condition
1              >10
1              <20
1              !=15
1              !=18
2              >30
并得出如下结果:

ID   Value   Rule_ID
1    11      1
2    60      2
3    15      null
我能想到的当前方法是使用像python这样的高级语言

  • 把规则都弄清楚
  • 创建where子句
  • 一个接一个地加入表格
  • 但是听起来效率很低,因为这意味着我需要将规则与数据表连接X次(X=规则总数)

    我想知道有没有更好的方法直接在SQLServer中实现这一点?有什么建议吗


    (同时假设规则之间没有冲突,这将使问题变得更加困难)

    无论在何处执行,都需要一种将数值与其规则表达式分离的方法(条件,如10) 1 < 20 将该集合连接到要检查/验证的信息集合。使用大量案例陈述

    case 
        when rule_type = '>' and value > other_value then true
        when rule_type = '>' and value <= other_value then false
        ...
    end as rule_satisfied
    
    案例
    当规则类型='>'和值>其他值时,则为true
    当rule_type='>'和value为TRUE时,则满足所有规则

    rule_details table:  
    
    t_rule_id rule_type Value
    1         >         10
    1         <         20
    
    case 
        when rule_type = '>' and value > other_value then true
        when rule_type = '>' and value <= other_value then false
        ...
    end as rule_satisfied