SQL: IF NULL THEN '-' ,ELSE condition

Mayur Kandalkar :

mo_orders table:

mo_name   admin_id
abcd        2
xyz        NULL

login_details table:

id     username
1       Mark
2       John
3       Bob

I wrote a SQL Statement similar like this (the original one looks different but I want to give an easy example here):

select ld.username 
from mo_orders mo inner join login_details ld on mo.admin_id=ld.id; 

This Statement does not have any syntax errors but how to write a statement when admin_id is NULL then print '-' else mo.admin_id=ld.id ?

Gordon Linoff :

If I understand correctly, you want left join with coalesce():

select mo.*, coalesce(ld.username, '-') as username
from mo_orders mo left join
     login_details ld 
     on mo.admin_id = ld.id; 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=391022&siteId=1