Created
February 15, 2021 07:32
-
-
Save anupam-io/6a718b97c5b28945cd91baf493c10810 to your computer and use it in GitHub Desktop.
How to set custom values instead of NULL in outer joins?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
drop database if exists db; | |
create database db; | |
use db; | |
drop table if exists a,b; | |
create table a(attr1 varchar(50), score int(10)); | |
insert into a (attr1, score)values | |
("info1", 1), ("info2", 2); | |
create table b(attr1 varchar(50)); | |
insert into b (attr1)values | |
("info1"), ("info2"), ("info3"), ("info4"); | |
-- start | |
select b.attr1, CASE WHEN a.score IS NULL THEN 0 ELSE a.score END as score | |
from a right join b | |
on a.attr1 = b.attr1 | |
; | |
-- end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: