catalog数据表中主要字段:id,name,parent_id,level;
SELECT
CASE
WHEN c.name IS NOT NULL THEN
c.name
WHEN b.name IS NOT NULL THEN
b.name
ELSE
a.name
END AS topName
FROM
catalog a
LEFT JOIN catalog b ON a.parent_id = b.id
LEFT JOIN catalog c ON b.parent_id = c.id
WHERE a.id = #{id,jdbcType=VARCHAR}
SELECT grade,
CASE
WHEN grade >= 90 THEN 'A'
WHEN grade >= 80 THEN 'B'
WHEN grade >= 70 THEN 'C'
WHEN grade >= 60 THEN 'D'
ELSE 'F'
END AS gpa
FROM scores;
在上面的示例中,使用了CASE WHEN语句,当grade大于等于90时返回’A’,当grade大于等于80时返回’B’,以此类推。最后使用了AS关键字给结果命名为gpa。