重命名数据?

pexxcrt2  于 2021-06-20  发布在  Mysql
关注(0)|答案(3)|浏览(340)

是否有方法设置/重命名查询返回的数据,我的查询如下所示:

userlevel   count
  1           5
  2           3
  3           11

SELECT USERLEVEL, COUNT(*) AS total FROM admin_xxx
GROUP BY USERLEVEL ORDER BY total DESC LIMIT 10
where client = %CURRENT_USER_LOGIN%

(最后一行是wordpress相关的-wpdatatables占位符)
我想重命名1为初级,重命名2为中级,重命名3为高级。我不想返回实际数字。
因此,如果返回的数据(level)等于=1->设置为junior。。。。。伯爵很好。
任何帮助或指导都将不胜感激。
谢谢

bkhjykvo

bkhjykvo1#

使用 CASE WHEN THEN ```
SELECT

CASE USERLEVEL
WHEN 1 THEN 'Junior',
WHEN 2 THEN 'Intermediate',
WHEN 3 THEN 'Senior'
ELSE 'Unknown'
END CASE as userlevel,

COUNT(*) AS total
FROM admin_xxx
GROUP BY userlevel
ORDER BY total;

b1payxdu

b1payxdu2#

SELECT IF(userLevel =1,'Junior',IF(userLevel =2,'Intermediate','Senior')) as levelname, ...

如果你有超过几个层次,我建议一个案例陈述

igetnqfo

igetnqfo3#

SELECT 
CASE 
WHEN USERLEVEL=1 THEN "Junior"
WHEN USERLEVEL=2 THEN "Intermdiate"
WHEN USERLEVEL=3 THEN "Senior"
END as UserLevel
, COUNT(*) AS total FROM admin_xxx
GROUP BY USERLEVEL ORDER BY total DESC LIMIT 10
where client = %CURRENT_USER_LOGIN%

相关问题