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 语句

多表联查

一张表左连接两次

相关推荐
tokepson4 小时前
Mysql下载部署方法备份(Windows/Linux)
linux·服务器·windows·mysql
nbsaas-boot6 小时前
SQL Server 存储过程开发规范(公司内部模板)
java·服务器·数据库
zgl_200537796 小时前
ZGLanguage 解析SQL数据血缘 之 Python + Echarts 显示SQL结构图
大数据·数据库·数据仓库·hadoop·sql·代码规范·源代码管理
acaad6 小时前
Redis下载与安装(Windows)
数据库·redis·缓存
玄〤6 小时前
黑马点评中 VoucherOrderServiceImpl 实现类中的一人一单实现解析(单机部署)
java·数据库·redis·笔记·后端·mybatis·springboot
SunflowerCoder7 小时前
EF Core + PostgreSQL 配置表设计踩坑记录:从 23505 到 ChangeTracker 冲突
数据库·postgresql·c#·efcore
短剑重铸之日7 小时前
《7天学会Redis》Day2 - 深入Redis数据结构与底层实现
数据结构·数据库·redis·后端
Zoey的笔记本8 小时前
「支持ISO27001的GTD协作平台」数据生命周期管理方案与加密通信协议
java·前端·数据库
什么都不会的Tristan8 小时前
MybatisPlus-扩展功能
数据库·mysql
超级种码8 小时前
Redis:Redis 数据类型
数据库·redis·缓存