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

多表联查

一张表左连接两次

相关推荐
正在走向自律21 小时前
电科金仓 KEMCC-V003R002C001B0001 在CentOS7系统环境内测体验:安装部署与功能实操全记录
数据库·国产数据库·电科金仓·kemcc新版本·kemcc
一 乐1 天前
数码商城系统|电子|基于SprinBoot+vue的数码商城系统(源码+数据库+文档)
java·前端·javascript·数据库·vue.js·springboot
Dillon Dong1 天前
【超详细】Ubuntu 上 MySQL 5.7 升级 MySQL 8 完整指南
linux·mysql·ubuntu
曹牧1 天前
Oracle:Replace
数据库·oracle
ZeroNews内网穿透1 天前
公网访问本地搭建开源在线流程图工具Draw.io
服务器·数据库·网络协议·安全·http·流程图·draw.io
m0_488777651 天前
MySQL数据库管理
数据库·mysql·数据库管理
曹牧1 天前
Oracle:数字转换为字符串
数据库·oracle
菜鸟小九1 天前
mysql高级(mysql管理)
数据库·mysql
高级盘丝洞1 天前
如何通过Powerlink协议读取PLC数据
开发语言·数据库·php
CrazyClaz1 天前
Sharding-JDBC
数据库·分库分表·sharding-jdbc