티스토리 뷰

# Quiz)
# 포인트가 2만 점보다 많은 유저만 뽑아보기!
select * from point_users
where point >= 20000

# 성이 황씨인 유저만 뽑아보기!
select * from users
where name = '황**'

# 웹개발 종합반이면서 결제수단이 CARD인 주문건만 뽑아보기!
select * from orders
where course_title = '웹개발 종합반' and payment_method = 'CARD'

# 꿀팁) show table로 어떤 테이블이 있는지 살펴보기
# select * from 테이블명 확인하기
# 원하는 정보가 없으면 다른 테이블에도 해보기
# 맞는 테이블을 찾았으면 조건을 걸 필드를 찾기
# select * from 테이블명 where 조건(필드명, and, or)


# where 절과 자주 같이 쓰는 문법 써보기
# between으로 설정 범위 내 데이터 출력 가능.
select * from orders
where created_at between '2020-07-13' and '2020-07-15'


# in으로 포함되는 데이터 출력 가능.
select * from checkins
where week in (1, 3)

# like로 패턴(문자열 규칙) 조건 걸어보기
select * from users
where email like '%daum.net'  # '%' 앞에 뭐가 오든 뒤에 있는 문자가 오는 데이터 출력.

# Quiz)
# 결제수단이 CARD가 아닌 주문 데이터만 추출해보기
select * from orders
where payment_method != 'CARD'

# 2만~3만 포인트 보유하고 있는 유저만 추출해보기
select * from point_users
where point between 20000 and 30000

# 이메일이 s로 시작하고 com으로 끝나는 유저만 추출해보기
select * from users
where email like 's%com'

# 이메일이 s로 시작하고 com으로 끝나면서 성이 이씨인 유저만 추출해보기
select * from users
where email like 's%' '%.com'and name = '이**'


# Limit를 사용해 일부 데이터만 가져오기(불러올 데이터가 많을 경우에 사용)
select * from orders
where payment_method = 'kakaopay'
limit 5

# distinct를 사용해 중복 데이터 제거하고 보기(데이터 필드에 어떤 종류가 있는지 알 수 있다.)
select distinct(payment_method) from orders

# count를 사용해 전체 데이터 수를 보기
select count(*) from orders
where payment_method = 'kakaopay'

# 응용 몇 개의 성이 있을까?
select count(distinct(name)) from users

 

-- 1-5 ~ 1-6 -------------------------------------------------------------------------
show tables

# 성이 남씨인 유저의 이메일만 추출하기
select * from users
where name = '남**'

select * from users
where name like '남**'

# gmail을 사용하는 2020/07/12~13에 가입한 유저를 추출하기
select * from users
where email like '%gmail.com' and created_at between '2020/07/12' and '2020/07/14'

# gmail을 사용하는 2020/07/12~13에 가입한 유저의 수를 세기
select count(*) from users
where email like '%gmail.com' and created_at between '2020/07/12' and '2020/07/14'

-- 1주 차 H.W -------------------------------------------------------------------------
# naver 이메일을 사용하면서, 웹개발 종합반을 신청했고 결제는 kakaopay로 이루어진 주문 데이터 추출하기 +a) 데이터의 수는?
select * from orders
where email like '%@naver.com'

select * from orders
where course_title = '웹개발 종합반'

select * from orders
where payment_method = 'kakaopay'

select * from orders
where email like '%@naver.com' and course_title = '웹개발 종합반' and payment_method = 'kakaopay'

select count(*) from orders
where email like '%@naver.com' and course_title = '웹개발 종합반' and payment_method = 'kakaopay'

 

 

REVIEW

SQL에 대해 처음 접해보는 시간이었다. 파이썬, C+과는 달리 적은 구문으로도 원하는 데이터를 뽑아낼 수 있는 점이 마음에 들었다. 익숙해진다면 실생활에 유용하게 적용해볼 수 있을 것 같다는 생각이 들었다.

계속해서 배운 것을 기반으로 어떤 프로젝트를 시작해볼지 고민해봐야겠다.

또한, 여러 구문을 자주 사용해보고 원하는 데이터를 얻는 연습을 반복해서 바로바로 쓸 수 있게 만들어야겠다.

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG more
«   2024/05   »
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 31
글 보관함