首页 > 解决方案 > SQL中解码函数内部的解码函数

问题描述

下面的sql语句是什么打印出来的,请解释一下:

SELECT DECODE (2, 2, DECODE(3, 7, 2, 4, 5, 6)) FROM DUAL

我只了解解码功能,但想确保我对解码功能中的解码的回答

标签: sqldecode

解决方案


它打印一个 6。

语法为 DECODE( 表达式 , 搜索 , 结果 [, 搜索 , 结果]... [, 默认] )

In your example 
    In the first DECODE 2 is the expression
    the next 2 is what the first 2 will be compared to
    the next DECODE is the result if 2 = 2

    In the second DECODE 3 is the expression
    It is compared to the 7 and the 4.
    The results could be 2 or 5
    Finally if the 3 doesn't match the 7 or the 4 then 6 is the result.
IF 2 = 2 THEN
  IF 3 = 7 THEN 
    2
  ELSIF 3 = 4 THEN
    5
  ELSE 6
END IF

推荐阅读