-
[MySQL] UNION, JOIN, SUB QUERYCSE/MySQL 2015. 6. 12. 16:25
이번 장을 진행하기전에 기존의 sale table을 삭제하고
아래 새로운 테이블을 작성한다.
12345678910111213141516171819202122create table sale(number varchar(10),sales int,month int);create table employ(number varchar(10),name varchar(10),age int);create table another_employ(number varchar(10),name varchar(10),age int);create table loc(number varchar(10),city varchar(5));cs 12345678910111213141516171819202122232425262728293031insert into sale values('A103', 101, 4);insert into sale values('A102', 54, 5);insert into sale values('A104', 181, 4);insert into sale values('A101', 184, 4);insert into sale values('A103', 17, 5);insert into sale values('A101', 300, 5);insert into sale values('A102', 205, 6);insert into sale values('A104', 93, 5);insert into sale values('A103', 12, 6);insert into sale values('A107', 87, 6);insert into employ values('A101', '강신우', 49);insert into employ values('A102', '김기덕', 28);insert into employ values('A103', '김민호', 20);insert into employ values('A104', '문소리', 23);insert into employ values('A105', '박문수', 35);insert into another_employ values('A106', '권명철', 26);insert into another_employ values('A107', '김우진', 24);insert into another_employ values('A108', '남수현', 23);insert into another_employ values('A109', '박지수', 25);insert into another_employ values('A110', '서연재', 27);insert into loc values('A101', '서울');insert into loc values('A102', '부산');insert into loc values('A103', '대구');insert into loc values('A104', '대전');insert into loc values('A105', '인천');cs 여러 개의 추출 결과를 함께 표시(UNION)
12345select column_name1 FROM table_name UNION SELECT column_name2 FROM table_name_1;SELECT * FROM employUNIONSELECT * FROM another_employ;cs 여러 개의 테이블 결합(Join)
12345678910SELECT column_nameFROM table_nameJOIN joined_table_nameON table_name.column = joined_table_name.columnselect *from saleJOIN employON sale.number = employ.number;cs 내부 조인(inner-Join)
12select * from sale INNER JOIN employ ON sale.number = employ.number;cs 결합한 테이블에 WHERE 조건 설정
12345select sale.number AS 사원번호, employ.name AS 이름, sale.sales AS 매출from salejoin employ USING(number)WHERE sale.sales >= 100cs 여러 테이블에서 데이터 추출
12345select sale.number, sale.sales, employ.name, loc.cityfrom salejoin employ using (number)join loc using (number)cs 하위 질의
평균 이상의 레코드 추출 - 하위 질의
1select * from employ where age >= (select avg(age) from employ)cs 'CSE > MySQL' 카테고리의 다른 글
[MySQL] 저장 프로시저(Stored Procedure) & 저장 함수(Stored Function) & 트리거(Trigger) (0) 2015.06.12 [MySQL] 뷰(View) (0) 2015.06.12 [MySQL] 수정(Update) & 삭제(Delete) (0) 2015.06.12 [MySQL] 조회 (0) 2015.06.12 [MySQL] 테이블 변경, 색인, 기본키 (0) 2015.06.12 [MySQL] 데이터베이스 & 테이블 (0) 2015.06.12