设置sql中where条件的默认参数

时间:2017-10-16 06:32:01

标签: mysql sql null

如果sql中的值为null,我需要使用默认值,如下所示。

SELECT sla.incident_generated_sla from eventedge_events_sla sla where e.client_id=sla.client_id and sla.priority_id=e.priority_id

我使用了以下查询但没有工作。

(SELECT sla.incident_generated_sla from eventedge_events_sla sla where e.client_id=sla.client_id and sla.priority_id=(IsNull(e.priority_id,"2")))

2 个答案:

答案 0 :(得分:0)

SELECT sla.incident_generated_sla from eventedge_events_sla sla 
where e.client_id=sla.client_id and sla.priority_id=e.priority_id

(SELECT sla.incident_generated_sla from eventedge_events_sla sla 
where e.client_id=sla.client_id and sla.priority_id=(IsNull(e.priority_id,"2")))

你几乎成功了。

根据您的尝试检查此更正:

SELECT sla.incident_generated_sla from eventedge_events_sla sla 
    where e.client_id=sla.client_id and sla.priority_id=ISNULL(e.priority_id,2)

现在我不知道你的“e”是什么,但是如果用你正在寻找的client_id声明它,它应该可以工作。

仔细看看我对你的ISNULL的更正。

编辑:评论后这个答案是正确的;

SELECT
    e.event_start_time,
    sla.incident_generated_sla

FROM
    eventedge_events_sla sla
    left outer join eventedge_events e 
    ON  e.client_id = sla.client_id 
        AND isnull(e.priority_id,2) = sla.priority_id

答案 1 :(得分:0)

它是IFNULL,如果它是MySQL。

SELECT sla.incident_generated_sla 
FROM eventedge_events_sla sla 
    LEFT JOIN eventedge_events e ON e.client_id = sla.client_id 
WHERE sla.priority_id = IFNULL(e.priority_id,"2");