LeetCode_sql_day21(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 复制代码
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')

分析:

①观察两个表 不妨将x和y的值 都加到expressions表中

此时就需要使用两个左连接

复制代码
select left_operand,v1.value as l,operator,right_operand ,v2.value as r
from Expressions left join Variables v1 on Expressions.left_operand = v1.name
                 left join Variables v2 on Expressions.right_operand = v2.name

②使用case when 根据操作符operator 列 判断 l 和 r 列值大小 并赋true 和false

复制代码
select
    left_operand,
    operator,
    right_operand,
             case
             when operator = '>' then if(l > r,'true','false')
             when operator = '<' then if(l < r,'true','false')
             when operator = '=' then if(l = r,'true','false')
         end value
    from t1;

代码:

sql 复制代码
with t1 as (
select left_operand,v1.value as l,operator,right_operand ,v2.value as r
from Expressions left join Variables v1 on Expressions.left_operand = v1.name
                 left join Variables v2 on Expressions.right_operand = v2.name)
select
    left_operand,
    operator,
    right_operand,
             case
             when operator = '>' then if(l > r,'true','false')
             when operator = '<' then if(l < r,'true','false')
             when operator = '=' then if(l = r,'true','false')
         end value
    from t1;

总结:

① 一开始走了一些弯路 将两个值连接后 用concat 连接 判断值 后来发现都是true

原因 是 concat连接的都当成字符串 无法计算

② case when 可以用两种解法

解法一:

找出所有true 的条件 然后 else false

case when operator = '>' and l>r then 'true'

when operator = '<' and l<r then 'true'

when operator = '=' and l=r then 'true'

else 'false' end

解法二:

另外一种就是上述代码中 then里面加个if判断 很巧妙 之前没用过

相关推荐
mit6.8242 分钟前
[贪心_7] 最优除法 | 跳跃游戏 II | 加油站
数据结构·算法·leetcode
老兵发新帖15 分钟前
pnpm常见报错解决办法
前端
Sonetto199923 分钟前
Nginx 反向代理,啥是“反向代理“啊,为啥叫“反向“代理?而不叫“正向”代理?它能干哈?
运维·前端·nginx
沐土Arvin23 分钟前
理解npm的工作原理:优化你的项目依赖管理流程
开发语言·前端·javascript·设计模式·npm·node.js
江沉晚呤时23 分钟前
深入了解C# List集合及两种常见排序算法:插入排序与堆排序
windows·sql·算法·oracle·c#·排序算法·mybatis
聪明的墨菲特i28 分钟前
SQL进阶知识:九、高级数据类型
xml·数据库·sql·mysql·json·空间数据类型
好_快38 分钟前
Lodash源码阅读-baseUniq
前端·javascript·源码阅读
不秃的开发媛39 分钟前
前端技术Ajax入门
java·开发语言·前端
牧羊狼的狼44 分钟前
React.memo 和 useMemo
前端·javascript·react.js
xixixin_1 小时前
【uniapp】vue2 搜索文字高亮显示
java·服务器·前端·uni-app·交互·文字高亮