我想让这个正常工作的代码更加美丽。
SET @weightClass = CASE
WHEN @totalWeight < 50 THEN 'A'
WHEN @totalWeight < 100 THEN 'B'
WHEN @totalWeight < 150 THEN 'C'
WHEN @totalWeight < 200 THEN 'D'
WHEN @totalWeight < 250 THEN 'E'
WHEN @totalWeight < 300 THEN 'F'
WHEN @totalWeight < 350 THEN 'G'
WHEN @totalWeight < 400 THEN 'H'
WHEN @totalWeight < 450 THEN 'I'
WHEN @totalWeight < 500 THEN 'J'
WHEN @totalWeight < 550 THEN 'K'
WHEN @totalWeight < 600 THEN 'L'
WHEN @totalWeight < 650 THEN 'M'
WHEN @totalWeight < 700 THEN 'N'
WHEN @totalWeight < 750 THEN 'O'
WHEN @totalWeight < 800 THEN 'P'
WHEN @totalWeight < 850 THEN 'Q'
WHEN @totalWeight < 900 THEN 'R'
WHEN @totalWeight < 950 THEN 'S'
ELSE 'T'
END
我认为可能有一种方法可以使用字母的ASCII值和50的逻辑增量。这可能(并且可行)吗?如何以及它会明显减慢查询速度?
答案 0 :(得分:7)
获取角色的功能是CHAR()
A的ascii代码是65
SELECT CASE
WHEN @totalWeight >= 950 THEN 'T'
ELSE CHAR(@totalWeight / 50 + 65)
END