我有2个表,其中包含工作人员和事件的列表。
当我试图带所有尚未完成的当前事件的工作人员列表时,我得到的是事件名称或NULL,但是我想获得空字符串,而不是null,因为当我将结果发送回HTML,屏幕上显示“ NULL”。
我尝试了
select workers.id, workers.name , events.name
from workers left join events on events.workerid = workers.id
where events.isfinished = false;
结果是:
1 Dave NULL
2 Charlie Event 2
3 Steve Event 3
我的桌子
Workers
Id Name
-----------------
1 Dave
2 Charlie
3 Steve
Events
Id Description workderId isFinished
------------------------------------------------------
1 Event 1 1 true
2 Event 2 2 false
3 Event 3 3 false
What should my sql be in order to get empty string or different value instead of NULL ?
答案 0 :(得分:1)
尝试添加COALESCE()
,如下所示:
SELECT workers.id, workers.name AS worker, COALESCE(events.name, '') AS event
FROM workers
LEFT JOIN events ON events.workerid = workers.id
WHERE events.isfinished = false
答案 1 :(得分:0)
使用COALESCE
:
SELECT
w.id,
w.name AS workers_name,
COALESCE(e.name, '') AS events_name -- or replace with some other string value
FROM workers w
LEFT JOIN events e
ON e.workerid = w.id
WHERE
e.isfinished = false;
请注意,您当前的查询正在选择两列,分别称为name
。我将它们别名为其他事物,以使结果集易于阅读。