计算布尔表达式的值
题目分析:题目中给了两张表,一张是x和y的值是多少,还有一张是x和y之间的关系,要求根据这两张表给出true或者false。
首先如果光有x和y而没有对应的值肯定不能进行比较,所以我们先得连接x和y对应的值,这个地方注意,不能使用inner join而应该使用left join。
接着要使用case when来判断两个变量的关系,而且需要根据给的符号的不同分别给出结果。
先进行连接得出最后要使用的表
select V1.value value1,E.*,V2.value value2 from Expressions E left join Variables V1 on E.left_operand = V1.name left join Variables V2 on E.right_operand = V2.name
在得出的要使用的表上面使用case when进行判断
select left_operand,operator,right_operand,
case operator
when ">" Then if(value1>value2,'true','false')
when "<" then if(value1<value2,'true','false')
when "=" then if(value1=value2,'true','false')
end value
from (
select V1.value value1,E.*,V2.value value2 from Expressions E left join Variables V1 on E.left_operand = V1.name left join Variables V2 on E.right_operand = V2.name) F1
提交结果如下: