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这样的高级语言
(同时假设规则之间没有冲突,这将使问题变得更加困难)无论在何处执行,都需要一种将数值与其规则表达式分离的方法(条件,如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