题目链接:点击打开链接
题目大意:略。
解题思路:解决方案(2)必须要用 UNION 不能使用 UNION ALL,因为需要去重。
AC 代码
-- 解决方案(1) select e.left_operand,e.operator,e.right_operand, case e.operator when '>' then if(v1.value>v2.value,'true','false') when '<' then if(v1.value<v2.value,'true','false') else if(v1.value=v2.value,'true','false') end value from Expressions e left join Variables v1 on v1.name = e.left_operand left join Variables v2 on v2.name = e.right_operand -- 解决方案(2) WITH t1 AS(SELECT name lvar, value lv, operator, right_operand FROM Expressions JOIN Variables ON left_operand = name), t2 AS(SELECT lvar, lv, operator, name rvar, value rv FROM t1 JOIN Variables ON right_operand = name), t3 AS( SELECT lvar, rvar, lv = rv 'value', '=' op FROM t2 UNION SELECT lvar, rvar, lv < rv 'value', '<' op FROM t2 UNION SELECT lvar, rvar, lv > rv 'value', '>' op FROM t2 ) SELECT left_operand, operator, right_operand, IF(value = 1, 'true', 'false') value FROM Expressions JOIN t3 ON left_operand = lvar AND right_operand = rvar AND operator = op