MySQL查詢--對查詢所得的欄位判斷並且重新賦值
阿新 • • 發佈:2021-10-14
對查詢所得的欄位進行條件判斷並且重新賦值
需求
當父節點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 '一級系統部件' ELSEa.`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_componentAS 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