MYSQL使用concat与case

时间:2014-03-03 09:17:30

标签: mysql case concat

SELECT CONCAT(firstname," ",lastname," ",CASE dept_id
WHEN  '1'    THEN  'ITD'
WHEN  '4'    THEN  'FMAD'
WHEN  '6'    THEN  'GCSMD'
WHEN  '7'    THEN  'SSHED'
WHEN  '10'   THEN  'GPD'
ELSE  'Z_ISDC'
END AS ayam) as staffname
FROM ost_staff

我想要staffname的输出是“John Doe ITD”但是 它不起作用。

2 个答案:

答案 0 :(得分:6)

试试这个,您的CASE格式不正确。

SELECT 
CONCAT(firstname," ",lastname," ",
CASE WHEN dept_id = '1' THEN 'ITD' 
     WHEN dept_id = '4' THEN 'FMAD' 
     WHEN dept_id = '6' THEN 'GCSMD' 
     WHEN dept_id = '7' THEN 'SSHED' 
     WHEN dept_id = '10' THEN 'GPD' ELSE 'Z_ISDC' END) 
as staffname 
FROM ost_staff

此外,您无法命名CASEEND需要一个简单的CASE

答案 1 :(得分:0)

AS ayam的{​​{1}}删除CASE

当您使用END子句的结果作为CASE的参数时,您不能使用别名。