mysql - 在mysql中使用if和else语句结果作为变量
问题描述
今天是个好日子,
在下面的 mysql 查询中使用 if 和 else 语句的结果作为变量时,我需要帮助:
SELECT type, description, IF(type = 'Fixed Assets', 'true', 'false') AS description, IF(description = description, 'true', 'false') AS description2 FROM table_name;
我需要的是在另一列“description2”中使用“description”的结果作为条件,这样我就不会一遍又一遍地使用相同的 IF 和 else 语句“description”。
description 的预期结果是“true”,而 description2 的预期结果也是“true”,因为 description 的值 = 'true'。
任何答案都会有很好的帮助,谢谢。
解决方案
只需重复您的IF(type...)
:
select
type,
description,
IF(type = 'Fixed Assets', 'true', 'false') AS description,
IF(description = IF(type = 'Fixed Assets', 'true', 'false'), 'true', 'false') AS description2
您的另一个选择是子查询:
select type, description, is_fixed_assets, IF(description = is_fixed_assets, 'true', 'false') as description2
from (
select type, description, IF(type = 'Fixed Assets', 'true', 'false') AS is_fixed_assets
from table_name
) foo;
推荐阅读
- ruby-on-rails - 如何从哈希中输出出现次数最多的电子邮件地址
- javascript - 当用户提交数据时,将用户输入的数据存储在 html 表中作为表的每一列的数组变量
- bootstrap-4 - 单击其他项目时菜单不会折叠
- operating-system - 如果操作系统看到的所有内容都是字节序列,为什么 Windows 不支持 ext4
- excel - VBA中的真正最后一列
- c++ - 使用 boost::program_options 的不带破折号的参数
- android - AndroidX 与 DataBinding Android 不兼容
- c# - 如何/在哪里获取失败的 FTP 身份验证日志?
- amazon-web-services - 在 EMR 上重启 presto-server 服务的正确方法
- python - 尝试使用 'distutil' 打包 C 模块时,此“注意:请参阅 '_PyInitError' 的声明”错误是什么意思?