table, field
table : 데이터 시트명
field : 각 데이터의 총칭(orders, user_id 등)
Select 쿼리문
select 쿼리문 : 데이터베이스에서 데이터를 선택해서 가져오겠다.
show tables
데이터베이스의 테이블 보기
select * from orders
orders 테이블의 모든 데이터(*)를 가져와보기
select email from orders
orders 테이블의 email 데이터만 가져와보기
Where절
select 쿼리문으로 가져올 데이터에 조건을 걺.
select * from orders where payment_method = 'kakaopay'
orders 테이블에서 payment_method가 'kakaopay'인 것만 가져와줘
* 반드시 문자열, 날짜는 ''을 넣기, 숫자는 넣지말기
select * from orders where course_title = '앱개발 종합반' and payment_method = 'kakaopay'
_orders 테이블에서 course_title이 앱개발 종합반이고, payment__method가 'kakaopay'인 것만 가져와줘
select * from point_users where point > 20000
포인트가 20000점보다 많은 유저만 뽑아줘
select * from users where name = '황**'
성이 황씨인 유저만 뽑아줘
where절과 자주 쓰이는 문법
!=
같지 않음(제외하기)
between 날짜 and 날짜
범위 정하기
ex) 2023-01-01 ~ 2023-01-03 사이의 데이터를 원할 경우, between '2023-01-01' and '2023-01-04(+1하기)'로 넣기
in
포함하기
ex) 1, 3주차의 데이터를 원할 경우, where week in (1,3)으로 넣기
like
패턴(문자열 규칙)
ex) where email like '%daum.net' 추출 시 앞의 내용이 어떻든 끝이 daum.net인 데이터만 추출
- where email like 'a%': email 필드값이 a로 시작하는 모든 데이터
- where email like '%a' email 필드값이 a로 끝나는 모든 데이터
- where email like '%co%' email 필드값에 co를 포함하는 모든 데이터
- where email like 'a%o' email 필드값이 a로 시작하고 o로 끝나는 모든 데이터
limit 숫자
데이터가 너무 방대할 경우, 원하는 숫자 만큼의 데이터만 가져오기
Distinct
중복 데이터 제외하기
select distinct(payment_method) from orders를 추출할 경우 payment_method에서 중복데이터를 제외한 데이터들이 나옴
count
몇 개인지 숫자 세보기
select count(*) from orders를 추출할 경우, 갯수만 나옴
* 만약 Distinct와 Count를 함께 쓸 경우, SELECT count(distinct(name)) from users로 적으면 됨.
오늘의 숙제
select * from orders
where email like '%naver.com'
and course_title = '웹개발 종합반'
and payment_method = 'kakaopay'