简体   繁体   English

多个条件下的案例陈述?

[英]Case statement in multiple conditions?

I want to check the column mr.name, if mr.name is null then i have to replace mr.name as mr.ticket_no.我想检查 mr.name 列,如果 mr.name 是 null,那么我必须将 mr.name 替换为 mr.ticket_no。 How?如何? Can use if else or case?可以使用 if else 或 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

When i use like this当我这样使用

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

it will throw an error它会抛出一个错误

if mr.name = null means i have to replace mr.name = mr.ticket_no.如果 mr.name = null 意味着我必须替换 mr.name = mr.ticket_no。 I want to check the column mr.name, if mr.name is null then i have to replace mr.name as mr.ticket_no我想检查 mr.name 列,如果 mr.name 是 null 那么我必须将 mr.name 替换为 mr.ticket_no

You can use coalesce() to replace null with anything:您可以使用 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

But if you are comfortable with case when then use as below:但是,如果您对大小写感到满意,请按以下方式使用:

 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM