简体   繁体   中英

SQL query case statement

I have an sql query which is generating a table

SELECT DISTINCT
concat(table.column, '.test.com) "User Login", 
concat(concat(table.columnname, ' '), lastname) "Full Name", 
'N' "Admin (Y/N)",
table.profile "Profile",
'Self' "admin", 
'Self' "user", 
FROM table.users

I want to add a case statement to the above query.

If (table.profile == admin){
admin = self;
user = null;
}
else{
admin = null;
user = self;
}

Can anyone help?

select  ...
,       case when profile = 'admin' then 'self' end as admin
,       case when coalesce(profile,'') <> 'admin' then 'self' end as user
from    table.users

The default value for a case is null, which meets your requirement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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