两种语法模式:
(1):只有一个分支
CASE
WHEN条件THEN结果1
ELSE结果2
END
(2):支持多种分支
CASE表达式1
WHEN表达式2THEN结果1
ELSE结果2
END
上面的WHEN可以重复多次,
===================
简单示例:
1.select
2.casewhen1=1
3.then1
4.else2
5.end;
1.select
2.case2
3.when1then1
4.when2then2
5.when3then3
6.else4
7.end;
============================
下面详细的各举几个例子,这些例子可以用于ESQL和交互式的SQL中
eg1:处理被0除
1.SELECT
2.CASE
3.WHEN fileld1=0 THEN 0
4.ELSE fileld2/field1
5.END
6.FROM FILE
eg2:字段值对应转换
1.SELECT ORDNO,CUSNAM,SHIPDATE,CASE
2. WHEN SHIPDATE<CURDATE() THEN 'OVERDUE'
3. WHEN SHIPDATE=CURDATE() THEN 'PROCESSING'
4. WHEN SHIPDATE>;CURDATE() THEN 'ACTIVE'
5. END
6.FROM FILE
eg3:获取月份的名字:上面的例子
Java代码
1.SELECT ORDNO,CUSNO,
2. CASE MONTH(SHIPDATE)
3. WHEN '01' THEN 'Jan'
4. WHEN '02' THEN 'Feb'
5. WHEN '03' THEN 'Mar'
6. WHEN '04' THEN 'Apr'
7. WHEN '05' THEN 'May'
8. WHEN '06' THEN 'Jun'
9. WHEN '07' THEN 'Jul'
10. WHEN '08' THEN 'Aug'
11. WHEN '09' THEN 'Sep'
12. WHEN '10' THEN 'Oct'
13. WHEN '11' THEN 'Nov'
14. WHEN '12' THEN 'Dec'
15. END
16.FROM FILE
SELECT ORDNO,CUSNO,
CASE MONTH(SHIPDATE)
WHEN '01' THEN 'Jan'
WHEN '02' THEN 'Feb'
WHEN '03' THEN 'Mar'
WHEN '04' THEN 'Apr'
WHEN '05' THEN 'May'
WHEN '06' THEN 'Jun'
WHEN '07' THEN 'Jul'
WHEN '08' THEN 'Aug'
WHEN '09' THEN 'Sep'
WHEN '10' THEN 'Oct'
WHEN '11' THEN 'Nov'
WHEN '12' THEN 'Dec'
END
FROM FILE
===================
CASE很好用,不仅SELECT可以用,UPATE里面也蛮灵
以上是关于查询的 下面再来个 update的
贴代码直观:
C-sharp代码
1.UPDATE properities
2.SET taxrate=
3. CASE
4. WHEN area<10000 THEN 0.05
5. WHEN area<20000 THEN 0.07
6. ELSE 0.09
7. END
UPDATE properities
SET taxrate=
CASE
WHEN area<10000 THEN 0.05
WHEN area<20000 THEN 0.07
ELSE 0.09
END
好了