多个条件下的案例陈述?

Case statement in multiple conditions?

我想检查列 mr.name,如果 mr.name 为空,则我必须将 mr.name 替换为 mr.ticket_no。如何?可以用if else or case吗?

    select ROW_NUMBER() OVER(ORDER BY mr_user) sl_no,* from (select 
    mr.name as mr_no,
    coalesce(mr.user_id,0) as mr_user
    from stock_production_lot lot
    left join kg_grn grn on (grn.name = lot.grn_no)
    left join kg_department_indent mr on (mr.name = grn.mr_no)
    order by mr.user_id) main
    where  mr_user=65

当我这样使用时

case when mr.name is null then '' else mr.ticket_no = grn.mr_no as mr_no

它会抛出一个错误

if mr.name = null 意味着我必须替换 mr.name = mr.ticket_no。我想检查列 mr.name,如果 mr.name 为空,则我必须将 mr.name 替换为 mr.ticket_no

您可以使用 coalesce() 将 null 替换为任何内容:

 select ROW_NUMBER() OVER(ORDER BY mr_user) sl_no,* from (select 
    coalesce(mr.name,mr.ticket_no)   as mr_no,
    coalesce(mr.user_id,0) as mr_user
    from stock_production_lot lot
    left join kg_grn grn on (grn.name = lot.grn_no)
    left join kg_department_indent mr on (mr.name = grn.mr_no)
    order by mr.user_id) main
    where  mr_user=65

但是如果你对 case when 感到满意,那么使用如下:

 select ROW_NUMBER() OVER(ORDER BY mr_user) sl_no,* from (select 
    (case when mr.name is null then mr.ticket_no else mr.name end)   as mr_no,
    coalesce(mr.user_id,0) as mr_user
    from stock_production_lot lot
    left join kg_grn grn on (grn.name = lot.grn_no)
    left join kg_department_indent mr on (mr.name = grn.mr_no)
    order by mr.user_id) main
    where  mr_user=65