use movie_app_db; insert into user (email, password, name, gender) values ('qqq2naver.com','1234','Mike',1); -- 여자는 0, 남자는 1 |
실습에서 사용할 데이터 입력
-- movie insert 데이터 : https://yeo0616.tistory.com/122
-- user insert 데이터 : https://yeo0616.tistory.com/123
-- review insert 데이터 : https://yeo0616.tistory.com/124
문제:
-- 1. 하트 눌러서, 즐겨찾기에 저장하는 SQL
-- 2. 다시 하트 누르면, 즐겨찾기에서 삭제하는 SQL
-- 3. 내 즐겨찾기 리스트 가져오는 SQL (9 페이지)
-- 4. 내가 작성한 리뷰 리스트 가져오는 SQL (8 페이지)
-- 5. 영화제목 검색하여, 영화 목록 가져오는 SQL (6페이지)
-- 6. 해당 영화에 대한 리뷰 리스트 가져오는 SQL (5페이지)
-- 7. 영화 상세 정보 가져오는 SQL (4페이지)
-- 1. 하트 눌러서, 즐겨찾기에 저장하는 SQL
insert into favorite (movie_id, user_id) values (5,2); |
-- 2. 다시 하트 누르면, 즐겨찾기에서 삭제하는 SQL
-- 즐겨찾기 해제 -- 테이블에서 데이터 삭제하면 된다.
delete from favorite where id = 3; select * from favorite; |
-- 3. 내 즐겨찾기 리스트 가져오는 SQL
-- 내 즐겨찾기 리스트 가져오기
select * from favorite where user_id =1; select m.title, count(r.movie_id) as cnt,ifnull(avg(r.rating),0) as avg from favorite f join movie m on m.id = f.movie_id left join review r on f.movie_id = r.movie_id group by m.title; select m.title, count(r.movie_id) as cnt,ifnull(avg(r.rating),0) as avg from favorite f join movie m on m.id = f.movie_id left join review r on f.movie_id = r.movie_id where f.user_id =1 group by m.title; |
ㄴ 여기까지가 모든 즐겨찾기에 관한 가공
ㄴ 없는 컬럼을 만들때만 having, 있는 컬럼을 가져와라 할 때는 where
-- 4. 내가 작성한 리뷰 리스트 가져오는 SQL
select u.email, u.name, if(u.gender=0,'여자','남자'),m.title, avg(r.rating) from user u join review r on u.id = r.user_id join movie m on m.id = r.movie_id where r.user_id = 1 group by m.title; -- > 쌤 풀이 select m.title, r.rating from review r join movie m on r.movie_id = m.id where r.user_id = 1; |
-- 5. 영화제목 검색하여, 영화 목록 가져오는 SQL
select m.title, count(r.id) as cnt, avg(r.rating) as avg from movie m join review r on m.id = r.movie_id group by m.title; -- > 쌤 풀이, 내가 무엇을 놓쳤는지 확인해보자 select m.title, count(r.movie_id) as cnt, ifnull(avg(r.rating),0) as avg from movie m left join review r on m.id = r.movie_id where m.title like '%star%' group by r.movie_id; |
ㄴ 영화가 나와야하니, 영화가 주인이다. 얘 옆다가 붙이자.
-- 6. 해당 영화에 대한 리뷰 리스트 가져오는 SQL
select u.name, r.content, r.rating -- 유저 이름, 유저 리뷰내용, 유저 별점 from review r join user u on r.user_id = u.id -- 리뷰 작성한 유저 정보까지 나오게 한다. where r.movie_id = 2; |
-- 7. 영화 상세 정보 가져오는 SQL (4페이지)
'프로그래밍 언어 > MySQL' 카테고리의 다른 글
[SQLD] 1. 데이터 모델링의 이해: 1. 데이터 모델링의 이해 (0) | 2022.10.14 |
---|---|
[MySQL] 다중 정렬, 정렬기준 2가지 이상일 때: order by (0) | 2022.08.30 |
[MySQL] 문자열 빨리 찾기: fulltext, 실습: 영화1 (0) | 2022.05.19 |
[MySQL] 실습: 일정 (0) | 2022.05.19 |
[MySQL] 실습: 좋아요 (0) | 2022.05.19 |