返回介绍

solution / 1400-1499 / 1440.Evaluate Boolean Expression / README_EN

发布于 2024-06-17 01:03:19 字数 4082 浏览 0 评论 0 收藏 0

1440. Evaluate Boolean Expression

中文文档

Description

Table Variables:

+---------------+---------+
| Column Name   | Type  |
+---------------+---------+
| name      | varchar |
| value     | int   |
+---------------+---------+
In SQL, 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 |
+---------------+---------+
In SQL, (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.

 

Evaluate the boolean expressions in Expressions table.

Return the result table in any order.

The result format is in the following example.

 

Example 1:

Input: 
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       |
+--------------+----------+---------------+
Output: 
+--------------+----------+---------------+-------+
| left_operand | operator | right_operand | value |
+--------------+----------+---------------+-------+
| x      | >    | y       | false |
| x      | <    | y       | true  |
| x      | =    | y       | false |
| y      | >    | x       | true  |
| y      | <    | x       | false |
| x      | =    | x       | true  |
+--------------+----------+---------------+-------+
Explanation: 
As shown, you need to find the value of each boolean expression in the table using the variables table.

Solutions

Solution 1: Equi-Join + CASE Expression

We can associate each row in the Expressions table with two rows in the Variables table using an equi-join, where the conditions for the association are left_operand = name and right_operand = name. Then, we can use a CASE expression to determine the value of the boolean expression. If the operator is =, we check if the two values are equal. If the operator is >, we check if the left value is greater than the right value. If the operator is <, we check if the left value is less than the right value. If the condition is true, the boolean expression evaluates to true, otherwise it evaluates to false.

# Write your MySQL query statement below
SELECT
  left_operand,
  operator,
  right_operand,
  CASE
    WHEN (
      (operator = '=' AND v1.value = v2.value)
      OR (operator = '>' AND v1.value > v2.value)
      OR (operator = '<' AND v1.value < v2.value)
    ) THEN 'true'
    ELSE 'false'
  END AS value
FROM
  Expressions AS e
  JOIN Variables AS v1 ON e.left_operand = v1.name
  JOIN Variables AS v2 ON e.right_operand = v2.name;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文