case..when..then语句
case..when..then语句
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*10elselayerindexend 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:
CASE是 SQL用来做为 if-then-else 之类逻辑的关键字。
//语法:
SELECT CASE ("栏位名")
FROM "表格名"
注:" 条件"
//例子:
//语句:select layername,case layertypewhen '点'then layerindex*2when '线'then layerindex*10elselayerindexend 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: