简体   繁体   English

MySQL更改标签值选择不同并计数

[英]MySQL Change label value in select distinct and count

I have the following query: 我有以下查询:

SELECT DISTINCT GRAD_ACAD,
COUNT(GRAD_ACAD) AS subtotal
FROM academicos
WHERE CCN REGEXP 'I|D65'
GROUP BY GRAD_ACAD
ORDER BY GRAD_ACAD

which results are: 结果是:

GRAD_ACAD  subtotal
DOC          11
LIC           2
MTR           3

What i want to do is to change the values labels into the following: 我要做的是将值标签更改为以下内容:

GRAD_ACAD      subtotal
Doctorado        11
Licenciatura      2
Maestría          3

I have tried Case when like this but i can't get it to work: 我已经尝试过Case了,但是却无法正常工作:

SELECT DISTINCT GRAD_ACAD CASE GRAD_ACAD WHEN 'DOC' THEN 'doctorado' ELSE(SELECT GRAD_ACAD) END AS GRAD_ACAD,
COUNT(GRAD_ACAD) AS subtotal
FROM academicos
WHERE CCN REGEXP 'I|D65'
GROUP BY GRAD_ACAD
ORDER BY GRAD_ACAD

How can i fix this? 我怎样才能解决这个问题?

case GRAD_ACAD when 'DOC' then 'Doctorado' when 'LIC' then 'Licenciatura' when 'MTR' then 'Maestria' else GRAD_ACAD end

您需要在SELECTGROUP BY中都放入相同的子句

No need to use DISTINCT here 无需在此处使用DISTINCT

SELECT  CASE GRAD_ACAD WHEN 'DOC' THEN 'doctorado' 
                 WHEN 'LIC' then 'Licenciatura'
                 WHEN 'MTR' then 'Maestria'
                 ELSE GRAD_ACAD end,
COUNT(GRAD_ACAD) AS subtotal
FROM academicos
WHERE CCN REGEXP 'I|D65'
GROUP BY 1
ORDER BY 1;

Notice that I used group by 1 and order by 1 which specify the first selected field, instead of repeating the case in both clause. 注意,我使用了group by 1order by 1来指定第一个选定字段,而不是在两个子句中都重复这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM