我之前问过类似的问题,但是我意识到我想要的输出完全不同。
我有以下查询
SELECT LEVEL, COUNT(*) AS total FROM admin_xxx
GROUP BY LEVEL ORDER BY total DESC LIMIT 10
表格输出
Level Total
4 27
3 24
7 19
5 10
8 9
6 7
1 5
2 3
现在我只想用单词而不是数字来重命名实际级别。
我想将其作为输出
Level Total
Bronze 27
Silver 24
Gold 19
Gold2 10
Gold3 9
Gold4 7
Gold5 5
Gold6 3
每10个级别都有一个单独的级别名称。
我尝试了以下操作,但语法错误
SELECT LEVEL, COUNT(*) AS total FROM admin_xxx
CASE
WHEN LEVEL = 1 THEN "Bronze",
etc...
WHEN LEVEL = 10 THEN "Gold10"
END as LEVEL
GROUP BY LEVEL ORDER BY total DESC LIMIT 10
答案 0 :(得分:0)
将您的GROUP BY
更改为
GROUP BY (
CASE
WHEN LEVEL = 1 THEN "Bronze",
etc...
WHEN LEVEL = 10 THEN "Gold10"
END
)
您的SELECT
上也存在语法错误
SELECT (
CASE
WHEN LEVEL = 1 THEN "Bronze",
etc...
WHEN LEVEL = 10 THEN "Gold10"
END
) AS LEVEL
, COUNT(*) AS total
FROM admin_xxx
GROUP BY (
CASE
WHEN LEVEL = 1 THEN "Bronze",
etc...
WHEN LEVEL = 10 THEN "Gold10"
END
)
ORDER BY total DESC LIMIT 10
答案 1 :(得分:0)
您的from子句位置错误
SELECT
CASE
WHEN LEVEL = 1 THEN "Bronze"
WHEN LEVEL = 10 THEN "Gold10"
END as LEVEL
, COUNT(*) AS total
FROM admin_xxx
GROUP BY LEVEL ORDER BY total DESC LIMIT 10
答案 2 :(得分:0)
您还可以将您的级别/标签分离到另一个表,并且可以将它们连接起来。 例如:
SELECT levels.level_label, COUNT(*) AS total
FROM admin_xxx
LEFT JOIN levels ON levels.label_value = admin_xxx.level
GROUP BY levels.level_label ORDER BY total DESC LIMIT 10
这是级别表:
id level_label label_value
1 Bronze 10
2 Silver 9
3 Gold 1
4 Gold2 2
5 Gold3 3
6 Gold4 4
7 Gold5 5
8 Gold6 6
9 Gold7 7
10 Gold8 8
答案 3 :(得分:0)
请尝试:显然,您应按照以下方式使用pagename
来获得所需的输出
CASE