[SQL] 1. Select, Where

샘샘이·2022년 9월 29일
0

일단 해봐! 해보고 오류 뜨면 그때 고치면 된다!!
5분이 되고 10분이 되고 한시간이 걸려도 내 힘으로 해보기


Select 쿼리문
데이터베이스에서 데이터를 선택해서 가져오는 것

WHERE 절
Select 쿼리문으로 가져올 데이터에 조건을 걸어주는 것


  • orders 테이블 불러오기 select * from orders
    paymethod가 'kakaopay'인 데이터 가져오기 where = ""
select * from orders
where payment_method = "kakaopay";

  • 여러 조건을 걸고 싶으면 and 쓰기
select * from orders
where course_title = "앱개발 종합반" and payment_method = "kakaopay";

  • 같지 않음을 나타내는 !=
select * from orders
where course_title != "웹개발 종합반";

  • 범위는 =을 쓰지 않고 betweenand를 쓴다
    (근데 문제 조건은 7/13~14일의 데이터인데 왜 15일까지 거는지 아직 이해 안됨)
select * from orders
where created_at between "2020-07-13" and "2020-07-15";

  • "1,3주차" 사람들만 보고 싶을 때는 in ( , )
select * from checkins 
where week in (1, 3);

  • daum 이메일을 쓰는 사람을 보고 싶을 때 '%daum'을 쓴다
    %daum%도 가능
select * from users 
where email like '%daum.net';

  • 조건에 맞는 데이터 5개만 보고 싶을 때는 limit 개수
select * from orders 
where payment_method = "kakaopay"
limit 5;

  • payment_method를 제외하고 보고 싶을 때 distinct(제외내용)
select distinct(payment_method) from orders;

  • orders 테이블의 데이터 개수를 세고 싶을 때 count()
select count(*) from orders

  • users 테이블에서 name을 제외한 데이터의 개수 count(distinct(제외내용))
SELECT count(distinct(name)) from users;
profile
할 수 있으니까 하고 있지!

0개의 댓글