首先,不要因为我尝试这样做而责备我 - 我的老板希望我将来自多个表的数据列表转储到一个 CSV 文件中。
此 CSV 包含两类人员,合并到一张表中:永久员工和承包商。
某些值的获取方式因您所属的类别而异。例如,如果您是承包商,您的职位描述在contractors.blended_role
中。如果您是永久性,职位描述位于 roles.blended_role
中,其中 roles.id = staff.role
。
所以我尝试了(类似的)这个:
SELECT
stuff,
(CASE
WHEN c.blended_role IS NULL THEN
r.blended_role
ELSE
c.blended_role
END) as `Job Description`,
morestuff
FROM
everyone e
LEFT JOIN contractors c
ON c.id = e.id
LEFT JOIN staff s
ON s.id = e.id
LEFT JOIN roles r
ON r.id = s.role
但是我遇到了语法错误
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'stuff, (CASE WHEN c.blended_role IS NULL THEN r.blended_role EL` at line 2
谁能提出替代方案?
更新
语法错误是由于缺少逗号导致的。
最佳答案
您没有名为 rv
和 cv
的表别名。也许你的意思是:
SELECT
(CASE
WHEN c.blended_role IS NULL THEN
r.blended_role
ELSE
c.blended_role
END) as `Job Description`
或者,更简单地说:
coalesce(c.blended_role, r.blended_role) . . .
关于mysql - 如果列值为空,则使用其他列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18512550/