250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- locals()
- CSS
- JS
- shuffle()
- inplace()
- glob
- discard()
- shutil
- __getitem__
- __sub__
- count()
- __len__
- choice()
- node.js
- items()
- fileinput
- remove()
- View
- MySqlDB
- randrange()
- decode()
- Database
- 파이썬
- MySQL
- 오버라이딩
- mro()
- __annotations__
- fnmatch
- zipfile
- HTML
Archives
- Today
- Total
흰둥이는 코드를 짤 때 짖어 (왈!왈!왈!왈!왈!왈!왈!왈!왈!왈!왈!)
(MySQL) 기초 문법(서브 쿼리) 본문
728x90
반응형
서브쿼리(Sub Query)
- 다른 쿼리 내부에 포함되어 있는 select 문을 의미
- 서브쿼리를 포함하고 있는 쿼리를 외부쿼리라고 부르고, 서브쿼리는 내부쿼리라고도 부름
- 서브쿼리는 괄호()로 감싸져서 표현
- 서브쿼리는 메인쿼리 컬럼 사용이 가능하며, 메인쿼리는 서브쿼리 컬럼을 사용하지 못함
- select, where, from, having 절 등에서 사용할 수 있음
product 테이블
select * from product;
where 절
# 100001의 가격보다 크거나 같은 price를 가지고 있는 상품의 모든 정보
select * from product where price >= (select price from product where code='100001');
select 절
# 코드, 이름, 가격, 전체 데이터의 가격중 가장 큰 값을 출력하는 쿼리
# 서브쿼리를 사용
# 나의 풀이
select code, name, price, (select price from product order by price desc limit 1) as 최대값 from product;
# 강사님 풀이
select code, name, price, (select max(price) from product) as max_price from product;
문제
- 상품을 최소 2개이상 구입한 회원의 아이디와, 이름, 성별을 출력
- 서브쿼리를 사용
member, orders 테이블
select * from member;
select * from orders;
나의 풀이 및 강사님 풀이
select userid, username, gender
from member
where userid in (select userid from orders group by userid having count(no) >= 2);
728x90
반응형
'MySQL' 카테고리의 다른 글
(MySQL) 기초 문법(테이블 복사) (0) | 2023.03.20 |
---|---|
(MySQL) 기초 문법(문자열 함수) (0) | 2023.03.20 |
(MySQL) 기초 문법(유니온) (1) | 2023.03.20 |
(MySQL) 기초 문법(auto_increment) (0) | 2023.03.20 |
(MySQL) 정규화 (0) | 2023.03.20 |
Comments