-
1440. 计算布尔表达式的值 - 力扣(LeetCode)
-
目标
-
输入
输入:表达式 left_operand operator right_operand x > y x < y x = y y > x y < x x = x 输入:值 name value x 66 y 77 -
输出
输出:布尔表达式 left_operand operator right_operand bool x > y FALSE x < y TRUE x = y FALSE y > x ture y < x FALSE x = x ture
-
-
分析
计算表 Expressions 中的布尔表达式。
返回的结果表 无顺序要求 。输入:表达式 输入:值 输出:布尔表达式 left_operand operator right_operand name value left_operand operator right_operand bool x > y x 66 x > y FALSE x < y y 77 x < y TRUE x = y x = y FALSE y > x y > x ture y < x y < x FALSE x = x x = x ture name value operator name value 内连接查询将x,y的值代入成一张表 left_operand operator right_operand bool case...when...条件判断operator的值和xy的比较输出布尔值 y 77 > x 66 x > y FALSE y 77 < x 66 x < y TRUE x 66 = x 66 x = y FALSE x 66 > y 77 y > x ture x 66 < y 77 y < x FALSE x 66 = y 77 x = x ture -
实现
DROP TABLE IF EXISTS Variables; DROP TABLE IF EXISTS Expressions; 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'); SELECT * FROM Expressions; SELECT * FROM Variables; SELECT # v1.*,operator,v2.* v1.name left_operand, operator, v2.name right_operand ,CASE WHEN (v1.value>v2.value AND operator='>')THEN 'true' WHEN (v1.value<v2.value AND operator='<')THEN 'true' WHEN (v1.value=v2.value AND operator='=')THEN 'true' ELSE 'false' END value FROM expressions e,variables v1,variables v2 WHERE left_operand=v1.name AND right_operand=v2.name;
-
小结
使用case...when条件查询