MySQL equivalent of DECODE function in Oracle(MySQL 相当于 Oracle 中的 DECODE 函数)
问题描述
我试图在 MySQL 中找到等效的 DECODE 函数.它是这样工作的:
I am trying to find an equivalent of DECODE function in MySQL. It works like this:
Select Name, DECODE(Age,
13,'Thirteen',14,'Fourteen',15,'Fifteen',16,'Sixteen',
17,'Seventeen',18,'Eighteen',19,'Nineteen',
'Adult') AS AgeBracket
FROM Person
DECODE 函数会将列 'Age' 的值与 13、14、15.. 进行比较,并返回适当的字符串值 'Thirteen'、'Fourteen'.. 如果没有匹配,则默认值为 'Adult'将被退回.
The DECODE function will compare value of column 'Age' with 13, 14, 15.. and return appropriate string value 'Thirteen', 'Fourteen'.. and if it matches with nothing, then default value of 'Adult' will be returned.
有什么想法可以在 MySQL 中的哪个函数中完成这项工作?谢谢.
Any ideas which function in MySQL can do this job? Thanks.
澄清:我同意使用 CASE 是实现预期结果的一种方式,但出于性能和其他原因,我更愿意寻找函数.
CLARIFICATION: I agree using CASE is one way of achieving desired result, but I am rather looking for a function because of performance and other reasons.
推荐答案
您可以使用 IF(),而在 Oracle 中您会使用 DECODE().
You can use IF() where in Oracle you would have used DECODE().
mysql> select if(emp_id=1,'X','Y') as test, emp_id from emps;
这篇关于MySQL 相当于 Oracle 中的 DECODE 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MySQL 相当于 Oracle 中的 DECODE 函数
基础教程推荐
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 无法解决整理冲突 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 在 SQL 中连接多个表 2021-01-01
