enkiki
enkiki

Reputation: 399

MySQL Query - Rename data?

Is there a way to set/rename data returned by a query, my query looks like:

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%   

(last line is WordPress related - wpdatatables placeholder)

I would like to rename 1 as Junior, rename 2 as Intermdiate and 3 as Senior. I don't want to return the actual number.

So if data returned (Level) equals = 1 -> set as Junior..... Count is fine.

Any help or guidance would be appreciated.

Thanks

Upvotes: 0

Views: 59

Answers (3)

Komal
Komal

Reputation: 1

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%  

Upvotes: 0

num8er
num8er

Reputation: 19372

Use 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;

Upvotes: 0

Bobert1234
Bobert1234

Reputation: 89

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

if you have more than a few levels, i suggest a CASE statment

Upvotes: 1

Related Questions