Leetcode1440. 计算布尔表达式的值(中等)

题目
Table Variables:

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| name          | varchar |
| value         | int     |
+---------------+---------+

name is the primary key for this table.
This table contains the stored variables and their values.

Table Expressions:

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| left_operand  | varchar |
| operator      | enum    |
| right_operand | varchar |
+---------------+---------+

(left_operand, operator, right_operand) is the primary key for this table.
This table contains a boolean expression that should be evaluated.
operator is an enum that takes one of the values ('<', '>', '=')
The values of left_operand and right_operand are guaranteed to be in the Variables table.

Write an SQL query to evaluate the boolean expressions in Expressions table.

Return the result table in any order.

The query result format is in the following example.

Variables table:

+------+-------+
| name | value |
+------+-------+
| x    | 66    |
| y    | 77    |
+------+-------+

Expressions table:

+--------------+----------+---------------+
| left_operand | operator | right_operand |
+--------------+----------+---------------+
| x            | >        | y             |
| x            | <        | y             |
| x            | =        | y             |
| y            | >        | x             |
| y            | <        | x             |
| x            | =        | x             |
+--------------+----------+---------------+

Result table:

+--------------+----------+---------------+-------+
| left_operand | operator | right_operand | value |
+--------------+----------+---------------+-------+
| x            | >        | y             | false |
| x            | <        | y             | true  |
| x            | =        | y             | false |
| y            | >        | x             | true  |
| y            | <        | x             | false |
| x            | =        | x             | true  |
+--------------+----------+---------------+-------+

As shown, you need find the value of each boolean exprssion in the table using the variables table.

解答
连接两表 需要两次

select V.value as left_value, V1.value as right_value,
E.*
from  Expressions as E
join Variables as V
on E.left_operand = V.name
joinVariables as V1
on E.left_operand = V1.name

判断

select tmp.left_operand, tmp.operator, tmp.right_operand,
(Case when operator = '>' then
        (Case when  left_value >= right_value then 'True' else 'False' end)
    when operator = '<' then
        (Case when  left_value <= right_value then 'True' else 'False' end)
    else
        (case when  left_value = right_value then 'True' else 'False' end)
    End) as value
from (select V.value as left_value, V1.value as right_value,
E.*
from  Expressions as E
join Variables as V
on E.left_operand = V.name
joinVariables as V1
on E.left_operand = V1.name) as tmp

你可能感兴趣的:(Leetcode1440. 计算布尔表达式的值(中等))