1440,计算布尔表达式的值

目录

计算布尔表达式的值

Variables:

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| name          | varchar |
| value         | int     |
+---------------+---------+
在 SQL 中,name 是该表主键.
该表包含了存储的变量及其对应的值.

Expressions:

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| left_operand  | varchar |
| operator      | enum    |
| right_operand | varchar |
+---------------+---------+
在 SQL 中,(left_operand, operator, right_operand) 是该表主键.
该表包含了需要计算的布尔表达式.
operator 是枚举类型, 取值于('<', '>', '=')
left_operand 和 right_operand 的值保证存在于 Variables 表单中.

计算表 Expressions 中的布尔表达式。

返回的结果表 无顺序要求

结果格式如下例所示。

示例 1:

输入:
Variables 表:
+------+-------+
| name | value |
+------+-------+
| x    | 66    |
| y    | 77    |
+------+-------+

Expressions 表:
+--------------+----------+---------------+
| left_operand | operator | right_operand |
+--------------+----------+---------------+
| x            | >        | y             |
| x            | <        | y             |
| x            | =        | y             |
| y            | >        | x             |
| y            | <        | x             |
| x            | =        | x             |
+--------------+----------+---------------+

输出:
+--------------+----------+---------------+-------+
| left_operand | operator | right_operand | value |
+--------------+----------+---------------+-------+
| x            | >        | y             | false |
| x            | <        | y             | true  |
| x            | =        | y             | false |
| y            | >        | x             | true  |
| y            | <        | x             | false |
| x            | =        | x             | true  |
+--------------+----------+---------------+-------+
解释:
如上所示, 你需要通过使用 Variables 表来找到 Expressions 表中的每一个布尔表达式的值.

数据准备

sql 复制代码
drop database if exists db_1;
create database db_1;
use db_1;

Create Table If Not Exists Variables (name varchar(3), value int);
Create Table If Not Exists Expressions (left_operand varchar(3), operator ENUM('>', '<', '='), right_operand varchar(3));
Truncate table Variables;
insert into Variables (name, value) values ('x', '66');
insert into Variables (name, value) values ('y', '77');
Truncate table Expressions;
insert into Expressions (left_operand, operator, right_operand) values ('x', '>', 'y');
insert into Expressions (left_operand, operator, right_operand) values ('x', '<', 'y');
insert into Expressions (left_operand, operator, right_operand) values ('x', '=', 'y');
insert into Expressions (left_operand, operator, right_operand) values ('y', '>', 'x');
insert into Expressions (left_operand, operator, right_operand) values ('y', '<', 'x');
insert into Expressions (left_operand, operator, right_operand) values ('x', '=', 'x');

Variables 表

Expressions 表

分析 + 实现

第一步:将 variables 表 左连接两次以取出相应的值

sql 复制代码
select
    *
from expressions e
-- 取出 比较符 左边的 值
left join variables v1 on e.left_operand = v1.name
-- 取出 比较符 右边的 值
left join variables v2 on e.right_operand = v2.name

第二步:计算布尔值

sql 复制代码
select
    e.left_operand,
    e.operator,
    e.right_operand,
    -- 使用 case when 进行判断
    case
        when e.operator = '>' and v1.value > v2.value then 'true'
        when e.operator = '<' and v1.value < v2.value then 'true'
        when e.operator = '=' and v1.value = v2.value then 'true'
        else 'false'
    end as value
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 语句

多表联查

一张表左连接两次

相关推荐
sekaii2 分钟前
ReDistribution plan细节
linux·服务器·数据库
焱焱枫38 分钟前
自适应SQL计划管理(Adaptive SQL Plan Management)在Oracle 12c中的应用
数据库·sql·oracle
2301_7930698241 分钟前
Spring Boot +SQL项目优化策略,GraphQL和SQL 区别,Spring JDBC 等原理辨析(万字长文+代码)
java·数据库·spring boot·sql·jdbc·orm
hhw1991121 小时前
spring boot知识点5
java·数据库·spring boot
m0_748232641 小时前
mysql的主从配置
android·mysql·adb
ITPUB-微风1 小时前
功能开关聚合对象实践:提升金融领域的高可用性
网络·数据库·金融
去看日出1 小时前
Linux(centos)系统安装部署MySQL8.0数据库(GLIBC版本)
linux·数据库·centos
Hanyaoo2 小时前
为什么mvcc中?m_ids 列表并不等同于 min_trx_id 和 max_trx_id 之间的所有事务 ID
数据库
偏右右2 小时前
PL/SQL 异常处理
数据库·sql·oracle
利瑞华3 小时前
Redis 存在线程安全问题吗?为什么?
数据库·redis·安全