日期:2014-05-16 浏览次数:20499 次
CASE是 SQL用来做为 if-then-else 之类逻辑的关键字。
//语法:
SELECT CASE ("栏位名")
FROM "表格名"
注:" 条件"
//例子:
//语句:select layername,case layertypewhen '点'then layerindex*2when '线'then layerindex*10else layerindexend newlayerindex,layerindexfrom th_geo_layerorder by layerindexasc
//结果:
//例子:古树名木按保护等级查询,保护等级的列表为汉字:一级,二级,三级
或者是:一级保护、二级保护、三级保护。(数据并不确定,故无法在控件中写死)。
//语句:string sSql = string.Format("select distinct ({0}) from {1} order by case when {0}='一级' then 1 when {0}='二级' then 2 when {0}='三级' then 3 else 1000 end",sFieldName,sTblName);
//结果:
//例子:古树名木按保护等级查询,保护等级的列表为汉字:一级,二级,三级
或者是:一级保护、二级保护、三级保护。(数据并不确定,故无法在控件中写死)。
//语句:string sSql = string.Format("select distinct ({0}) from {1} order by case when {0}='一级' then 1 when {0}='二级' then 2 when {0}='三级' then 3 else 1000 end",sFieldName,sTblName);
//结果:
//语句2:select distinct layertype fromth_geo_layer order by case layertype when '点' then 1when '线' then 2 when '面' then 3when '注记' then 4 else null end
//结果2: