场景
在上面中使用的查询jsTree数据的语句为:
SELECT
id,
ISNULL(
CAST (
pid AS VARCHAR ( 50 )),
CAST (
'#' AS VARCHAR ( 50 ))) parent,
icon,
text,
num,
org_classify,
is_top_level,
CASE
WHEN id = #{selectedId}
THEN 1
ELSE 0
END selected,
opened,
disabled,
creator,
modifier
FROM
sys_enterprise_org
WHERE
is_delete = 0
ORDER BY
id ASC
实现
解读:
1.ISNULL(a,b)
如果左边为空就用右边代替。
在树形结构中如果是顶级结构则pid即负极节点为空,所以使用#代替。
2.CAST (pid AS VARCHAR ( 50 ))
是类型转换,将左边的类型转换成右边的类型。
3.CASE
WHEN id = #{selectedId}
THEN 1
ELSE 0
END selected,
类似于switch case 语句
当id = #{selectedId} 时返回1,否则返回0,将返回值作为selected的值。
标签:CASE,VARCHAR,Sqlserver,selected,pid,CAST,id From: https://blog.51cto.com/BADAOLIUMANGQZ/6115097