需求
当父节点id等于0时,父节点的名称显示为"一级系统部件"
解决方案
使用case when then
语法
CASE 字段
WHEN 条件1 THEN 值 1
WHEN 条件2 THEN 值 2
[WHEN ...]
[ELSE 值]
END 别名
实例
当父节点id等于0时,父节点的名称显示为"一级系统部件",其他显示为对应的名称
SELECT vc.id AS id, vc.`name` AS `name`, vc.parent_id AS parentId, CASE vc.parent_id WHEN 0 THEN '一级系统部件' ELSE a.`name` END parentName, vc.material_number AS materialNumber, vc.structure_type AS structureType, sd.`name` AS structureTypeName, vc.type AS type, vc.sort AS sort, vc.line_id AS lineId, b.`name` AS lineName, vc.`level` AS `level`, vc.remark AS remark FROM (( vehicle_component AS vc LEFT JOIN sys_dict AS sd ON sd.`code` = vc.structure_type ) LEFT JOIN vehicle_component AS a ON a.id = vc.parent_id ) LEFT JOIN line AS b ON b.id = vc.line_id WHERE vc.`status` = 1 AND vc.line_id = 49