在oracle中NVL(表达式1,表达式2)
NVL(E1, E2)的功能为:如果E1为NULL,则函数返回E2,否则返回E1本身
其表达式的值可以是数字型、字符型和日期型。但是表达式1和表达式2的数据类型必须为同一个类型。
对于在MySQL中有ifnull对应
IFNULL(expression_1,expression_2);
如果expression_1不为NULL,则IFNULL函数返回expression_1; 否则返回expression_2的结果。
例子:
oracle中的decode()
decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值)
IF 条件=值1 THEN
RETURN(翻译值1)
ELSIF 条件=值2 THEN
RETURN(翻译值2)
......
ELSIF 条件=值n THEN
RETURN(翻译值n)
ELSE
RETURN(缺省值)
END IF
decode(字段或字段的运算,值1,值2,值3)
MySQL中就没有decode类似的方法,所以只能用上面的if方法,或者case when 条件1 then 翻译值1 when 条件2 then 翻译值2 else 缺省值
例子:
SELECT (@i:=@i+1) id,t.id num_id, t.value SCHOOL_ID, t.label SCHOOL, a.NUMS,a.TJN FROM sys_dict t
LEFT JOIN (select f.v_code, COUNT(1) NUMS, SUM(CASE WHEN v.VERIFY_STATUS = '3' THEN 1 WHEN '0' THEN 1 ELSE 0 END) tjn from T_PG_EXP_COUNT f
LEFT JOIN T_PG_OUT_EXPERTS_VERIFY v ON f.EXPERTS_VERIFY_ID = v.NUM_ID
, (select @i:=0) as it
WHERE f.TYPE = 1 GROUP BY f.v_code) a
ON t.VALUE = a.v_code where t.type='T_COMMON_SCHOOL' order by t.value
oracle的写法:
SELECT rownum id,t.id num_id, t.value SCHOOL_ID, t.label SCHOOL, a.NUMS,a.TJN
FROM sys_dict t
LEFT JOIN (select f.v_code, COUNT(1) NUMS, SUM(DECODE(v.VERIFY_STATUS,'3',1,'0',1,0)) tjn from T_PG_EXP_COUNT f
LEFT JOIN T_PG_OUT_EXPERTS_VERIFY v ON f.EXPERTS_VERIFY_ID = v.NUM_ID
WHERE f.apply_id = #{APPLY_ID} AND f.TYPE = 1 GROUP BY f.v_code) a
ON t.VALUE = a.v_code