说我有一张桌子即
___________________
| ID | Name | Job |
+====+======+=====+
| 01 | Fred | br |
+====+======+=====+
| 02 | Jack | pd |
+====+======+=====+
我运行像
这样的查询SELECT
CASE Job
WHEN 'br' THEN 'cleaner'
WHEN 'pd' THEN 'teacher'
ELSE Job
END as new_job
CONCAT( Name, 'is a', new_job )
FROM table
我收到错误,因为我无法在我的concat中使用new_job,无论如何都要这样做?
事先提醒:)
答案 0 :(得分:3)
将其包装为子查询:
SELECT CONCAT( Name, 'is a', new_job ) FROM(
SELECT Name,
CASE Job
WHEN 'br' THEN 'cleaner'
WHEN 'pd' THEN 'teacher'
ELSE Job
END as new_job
FROM table ) sub
答案 1 :(得分:0)
除了“换行”之外,您还可以使用CASE
函数中的CONCAT()
:
SELECT
CONCAT( Name
, 'is a'
, CASE Job
WHEN 'br' THEN 'cleaner'
WHEN 'pd' THEN 'teacher'
ELSE Job
END
) AS result
FROM table