case..when..then

来源:互联网 发布:qq飞车淘宝网商城 编辑:程序博客网 时间:2024/04/20 13:58

CASE是 SQL用来做为 if-then-else 之类逻辑的关键字。

//语法:

SELECT CASE ("栏位名")

    WHEN " 条件1" THEN "结果1"

    WHEN " 条件2" THEN "结果2"

    ...

    [ELSE " 结果N"]

    END

FROM "表格名"

注:" 条件"  可以是一个数值或是公式。  ELSE  子句则并不是必须的。

 

 

//例子:

//语句: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:





原创粉丝点击