left join pokemon on trainer.trainer_type=pokemon.type and trainer.id = 1에서 trainer id=1 인 행들만 반환될까?
-> 아니오. trainer.id이 1이 아닌 행들도 반환된다.
on절에서 조건을 만족하지 못한 trainer 행들은 포켓몬 컬럼들이 null로 채워져서 반환된다.
create table pokemon (
id INT auto_increment primary key,
name varchar(20),
type varchar(20)
);
create table trainer (
trainer_id BIGINT primary key,
trainer_name varchar(20),
trainer_type varchar(20)
);
| id | name | type |
|---|---|---|
| 1 | pikachu | electric |
| 2 | balbusaur | grass |
| 3 | pikachu | electric |
| 4 | coil | electric |
| trainer_id | trainer_name | trainer_type |
|---|---|---|
| 1 | ash | electric |
| 2 | martin | electric |
| 3 | hyuaryun | fire |
select * from trainer left join pokemon
on trainer.trainer_type=pokemon.type
and trainer.id=1
| trainer_id | trainer_name | trainer_type | id | name | type |
|---|---|---|---|---|---|
| 1 | ash | electric | 1 | pikachu | electric |
| 1 | ash | electric | 3 | pikachu | electric |
| 1 | ash | electric | 4 | coil | electric |
| 2 | martin | electric | NULL | NULL | NULL |
| 4 | hywaryun | fire | NULL | NULL | NULL |
정확히 trainer.id=1인 행들만 나오기를 원한다면, on 절이 아닌 where절에 조건을 넣어야 한다.
예시:
select * from trainer left join pokemon
on trainer.trainer_type=pokemon.type
where trainer.id=1