-- 서브 쿼리(서브쿼리 where, 인라인뷰 from, 스칼라서브쿼리 select)
-- 스칼라(1)
-- 벡터(1,2) 2차원 벡터
-- 벡터(3,4,5) 3차원 벡터
기본 서브쿼리
-- 1. 기본 서브쿼리 where(select, select 두번 이상 나오면)
select *
from emp
where deptno = 20;
select deptno
from dept
where loc = 'DALLAS';
select *
from emp
where deptno = (select deptno
from dept
where loc = 'DALLAS');


인라인 뷰
-- 2. 인라인 뷰
select *
from (select ename, sal * 12 allsal
from emp) e
where allsal = 9600;
스칼라서브쿼리
-- 3. 스칼라서브쿼리
select ename, sal , (select sal * 12
from emp
where sal * 12 = 9600)
from emp
where job = 'salesman';
예제
-- 메인
select ename, sal
from emp
where sal = 5000;
-- 서브
select max(sal)
from emp;
-- 합치기
select ename, sal
from emp
where sal = (select max(sal)
from emp);


select ename, sal
from emp;
select round(avg(sal))
from emp;
select ename, sal,(select avg(sal)from emp) - sal
from emp;
select ename, sal,(select avg(sal) - e.sal from emp)
from emp e;


-- 각 사원의 이름(ename, deptno, dname)을 출력하시오
select ename, deptno, (select dname
from dept
where deptno = e.deptno)
from emp e;

-- 각 사원의 이름(ename), 급여(sal) 그리고 해당 사원이 속한 부서의 평균 급여를 출력하라
select ename, sal, (select avg(sal)
from emp
where deptno = e.deptno) 평균급여
from emp e;

create database insta;
use insta;
CREATE TABLE member (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(20),
password VARCHAR(20),
user_img VARCHAR(100),
email VARCHAR(100),
create_date DATETIME,
update_date DATETIME
) ENGINE=InnoDB
DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_unicode_ci;
CREATE TABLE image (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
content VARCHAR(1000),
img_url VARCHAR(100),
user_id BIGINT,
create_date DATETIME,
update_date DATETIME
) ENGINE=InnoDB
DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_unicode_ci;
CREATE TABLE favorite (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
image_id BIGINT,
user_id BIGINT,
create_date DATETIME,
update_date DATETIME
) ENGINE=InnoDB
DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_unicode_ci;
CREATE TABLE tag (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
content VARCHAR(30),
image_id BIGINT,
create_date DATETIME,
update_date DATETIME
) ENGINE=InnoDB
DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_unicode_ci;
INSERT INTO member(username, password, user_img, email, create_date, update_date)
VALUES
('ssar', '1234', NULL, 'ssar@nate.com', NOW(), NOW()),
('cos', '1234', NULL, 'cos@nate.com', NOW(), NOW()),
('love', '1234', NULL, 'love@nate.com', NOW(), NOW());
INSERT INTO image (img_url, content, user_id, create_date, update_date)
VALUES ('D드라이브', '여행사진', 1, NOW(), NOW());
INSERT INTO image (img_url, content, user_id, create_date, update_date)
VALUES ('C드라이브', '강아지사진', 1, NOW(), NOW());
INSERT INTO image (img_url, content, user_id, create_date, update_date)
VALUES ('E드라이브', '친구사진', 2, NOW(), NOW());
INSERT INTO favorite (user_id, image_id, create_date, update_date)
VALUES (1, 1, NOW(), NOW());
INSERT INTO favorite (user_id, image_id, create_date, update_date)
VALUES (2, 2, NOW(), NOW());
INSERT INTO favorite (user_id, image_id, create_date, update_date)
VALUES (2, 3, NOW(), NOW());
INSERT INTO tag (content, image_id, create_date, update_date)
VALUES ('가족여행', 1, NOW(), NOW());
INSERT INTO favorite (user_id, image_id, create_date, update_date)
VALUES (2, 1, NOW(), NOW());
INSERT INTO favorite (user_id, image_id, create_date, update_date)
VALUES (3, 1, NOW(), NOW());
INSERT INTO favorite (user_id, image_id, create_date, update_date)
VALUES (1, 2, NOW(), NOW());
select * from favorite;
select * from image;
select * from member;
select * from tag;
delete from favorite where id = 4;
delete from favorite where id = 5;
delete from favorite where id = 6;
-- 이미지 출력 시 옆에 좋아요 표시하기
select id, img_url, content,
(select count(user_id) from favorite where image_id = im.id) as '종아요개수',
(select username from member where id = im.user_id) as '유저이름'
from image im;
-- 좋아요 전체 테이블 확인
select *
from favorite;
-- 특정 이미지에 대한 좋아요 개수 확인
select count(user_id)
from favorite
where image_id = 1;
-- 멤버 전체 테이블 확인
select *
from member;
-- 특정 넘버인 멤버의 이름 확인
select username
from member
where id = 1;Share article