SQL - 1주차, 기본 문법

0

SQL

목록 보기
1/11

Select 쿼리문에 where 절 사용하는 방법

  1. show tables 또는 왼쪽 목록에서도 table name들을 확인할 수 있다.

select * from <table name>
where <해당 필드 이름> = "<찾고자 하는 데이터>" and  <해당 필드 이름2> = "<찾고자 하는 데이터2>"

# 예시 1
select * from point_users
where point > 20000;

# 예시 2
select * from orders
where course_title = "웹개발 종합반" and payment_method = "CARD";
< where 절과 자주 쓰는 문법 >

1. 같지 않음 : !=

select * from orders
where course_title != "웹개발 종합반";

2. 범위 : between

select * from orders
where created_at between "2020-07-13" and "2020-07-15";

3. 포함 : in

select * from checkins 
where week in (1, 3); // week의 값은 1, 2, 3 .. 숫자로만 등록 돼 있음

4. 패턴 (문자열 규칙) : like

select * from users 
where email like '%daum.net';

# like의 사용법
where email like 'a%': email 필드값이 'a로 시작하는' 모든 데이터
'%a' : email 필드값이 'a로 끝나는' 모든 데이터
'%co%' : email 필드값에 co를 '포함'하는 모든 데이터
'a%o' : email 필드값이 'a로 시작하고 o로 끝나는' 모든 데이터

SQL 유용한 문법

1. Limit : 데이터를 불러오는데 시간을 줄이기 위해서 제한을 둠

select * from orders 
where payment_method = "kakaopay"
limit 5;  // 5개만 보이게

2. Distinct : 중복 데이터 제외하고 가져오기 - 1개씩만(구분지을 수 있음)

select distinct(payment_method) from orders;

3. 갯수 세보기

select count(*) from orders
# distinct 와 count 같이 써보기
select distinct(name) from users;

< 퀴즈 >

Gmail을 사용하는 2020/07/12~13에 가입한 유저를 추출하기

select count(*) from users
where email like '%gmail.com' and
created_at BETWEEN "2020-07-12" and "2020-07-13"  // where을 한번 더 안쓰고 and 로 연결해줘도 됨
profile
백엔드를 공부하고 있습니다.

0개의 댓글

관련 채용 정보