废话不多,直接上SQL
case 形式一:
SELECT f.series_id, <!-- f.fuel_flag, --> (CASE f.fuel_flag WHEN 1 THEN ‘汽油‘ WHEN 2 THEN ‘柴油‘ WHEN 3 THEN ‘油电混合‘ WHEN 4 THEN ‘纯电动‘ WHEN 5 THEN ‘油气混合‘ WHEN 6 THEN ‘液化石油气‘ WHEN 7 THEN ‘压缩天然气‘ WHEN 8 THEN ‘甲醇‘ WHEN 9 THEN ‘乙醇‘ ELSE ‘‘ END ) as fuel_flag, f.car_age FROM series_fuel_flag f
case 形式二:
SELECT f.series_id, <!-- f.fuel_flag, --> (CASE WHEN f.fuel_flag = 1 THEN ‘汽油‘ WHEN f.fuel_flag = 2 THEN ‘柴油‘ WHEN f.fuel_flag = 3 THEN ‘油电混合‘ WHEN f.fuel_flag = 4 THEN ‘纯电动‘ ...... ELSE ‘‘ END ) as fuel_flag, f.car_age FROM series_fuel_flag f
形式二比形式一要灵活 “=” 可以换成其他表达式 如 “>,<” 等 还 可以
(case
WHEN f.fuel_flag in (1,2,3) THEN ‘非电动‘
ELSE ‘‘ END
)但是如果是
(case
WHEN f.fuel_flag in (1,2,3) THEN ‘非电动‘
WHEN f.fuel_flag in (1,2,3) THEN ‘燃油‘
ELSE ‘‘ END
)
只能获取到 非电动 而获取不到 燃油
原文地址:https://www.cnblogs.com/xingtangxiaoga/p/9714940.html
时间: 2024-10-03 07:16:34