use world;
select world;
CREATE TABLE user (
user_id int(11) unsigned NOT NULL AUTO_INCREMENT,
name varchar(30) DEFAULT NULL,
PRIMARY KEY (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE addr (
id int(11) unsigned NOT NULL AUTO_INCREMENT,
addr varchar(30) DEFAULT NULL,
user_id int(11) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO user(name)
VALUES (“jin”),
(“po”),
(“alice”),
(“petter”);
INSERT INTO addr(addr, user_id)
VALUES (“seoul”, 1),
(“pusan”, 2),
(“deajeon”, 3),
(“deagu”, 5),
(“seoul”, 6);
— inner join
select id, user.name, addr.addr
from user
join addr
where user.user_id = addr.user_id;
— on user.user_id=addr.user_id;
— 원리
select *
from user
join addr;
— inner join
select id, user.name, addr.addr
from user
join addr
on user.user_id=addr.user_id;
— 문제 world의 도시이름과 도시의 국가 이름출력
— 국가코드, 도시이름, 국가이름
select *
from city
join country
on city.id=country.id;
— 답
select city.CountryCode, city.name as city_name, country.name as county_name
from city
join country
on city.CountryCode = country.code;
— left join
— 왼쪽 테이블 기준 join; 왼쪽 테이블 데이터는 모두 출력
— 키값이 매핑되는 데이터가 없으면 Null 출력
— 0은 if문으로!
select id, user.name, addr.addr
— user가 left임
from user
left join addr
— addr의 id가 null
on user.user_id = addr.user_id;
— left가 addr
select id, user.name, addr.addr
— user가 left임
from addr
left join user
— user의 id가 null
on user.user_id = addr.user_id;
— right
select id, us거er.name, addr.addr
from addr
right join user
on user.user_id = addr.user_id;
— union
— select 쿼리의 결과를 합쳐서 출력하는 방법
— 자동으로 중복데이터 제
— full outer join 구현
select name
from user
— union all은 중복 제거 안함
union all
select addr
from addr;
— full outer join
select addr.id, user.name, addr.addr
from user
left join addr
on user.user_id = addr.user_id;
select addr.id, user.name, addr.addr
from user
right join addr
on user.user_id = addr.user_id;
— 합친다
select addr.id, user.name, addr.addr
from user
left join addr
on user.user_id = addr.user_id
union
select addr.id, user.name, addr.addr
from user
right join addr
on user.user_id = addr.user_id;
— subquery
— query안에 query
use world;
— 1. select절 sub query: world 데이터 베이스에서 전체 나라수 & 도시수 & 언어수 출력
select
(select count(name) from city) as total_city,
— count에 가장 편한 애를 자기가 선택
(select count(*) from country) as total_country,
(select count(distinct(Language)) from countrylanguage) as total_country
— from 없어도 될 때
from dual;
— 2. from절 sub query
— 800만 이상 도시의 국가코드, 국가이름, 도시이름, 도시인구수 출력
— 1)join해서 필터링하는 방법 2)where로 선별 다시 join
— join은 느리니까 적은 데이터로 join 하자
— 틀립 —————————————————-
select *
from
(select countrycode, name, population
from city
— alias 있어야 함
where population > 8000000) as city
join
(select code, name
from country) as country
on country.code = city.countrycode;
— where sub-query
— 800만 이상 도시의 국가코드, 국가이름, 대통령 이름 출력
— 틀림———————————————————————————-
select code, name, HeadOfState
from country
where code in(
— 800 만이상
select countrycode
from city
where Population > 8000000
);
— 문제1 틀림
select
from country;
select name
from country
where
(select population from exico;
— 문제1 답
— 나라 이름과 인구수 필요
select name, population
from country
— where population > 멕시코 인구수보다 큰
where population >(
select population
from country
where name=”Mexico”
)
order by population desc;
— 문제2: city와 country join
— 1)
select country.code, country.name, city.name
from city
join country
on city.CountryCode = country.Code;
select country.name, count(country.name) as count
from city
join country
on city.CountryCode = country.code
group by country.name
order by count desc;