Logical Operation : 논리 연산자
AND = 조건 모두 만족할 때 true 리턴
- 여러 개의 조건을 걸 때 사용할 수 있음
<기본 구조>
SELECT col1, col2
FROM tablename
WHERE condition1 AND condition2 AND condition3
select *
from celeb
where sex='M' and age>40
order by name desc;
OR = 하나의 조건이라도 만족하면 true 리턴
<기본 구조>
SELECT col1, col2
FROM tablename
WHERE condition1 OR condition2 OR condition3
select *
from celeb
where age<25 or age>30
order by age;
🚨 OR 와 AND 둘 다 활용
🚨 'OR'는 조건 중 하나라도 충족하면 검색 및 조회 가능
select *
from celeb
where (age<29 AND sex='F') OR where (age>30 AND sex='M')
order by age, sex;
🚨 AND > OR 순으로 AND가 먼저 처리됨!
그래서 먼저 처리되어야 하는 조건을 () 괄호 처리!
select *
from celeb
where (agency='yg엔터테인먼트' or agency='나무엑터스') and age<30;
🚨 먼저 처리되어야 하는 조건 = 괄호!!
- IF 괄호가 없다면? = ERROR!!
--> 에어전시가 나무엑터스이고 나이가 30세 미만 이거나 yg 엔터인 데이터 추출됨.
select *
from celeb
where ((id%2) = 1 and sex = 'M') or ((id%2) = 0 and agency = 'yg엔터테인먼트')
order by age;
🚨홀수 / 짝수 구하는 조건문!!!
홀수 : (id%2) = 1 짝수 : (id%2) = 0
NOT = 조건을 만족하지 않는 경우 true 리턴
<기본 구조>
🚨 조건 앞에 NOT !!
SELECT col1, col2
FROM tablename
WHERE NOT condition1
select *
from celeb
where NOT sex='F';
select *
from celeb
where (agency='yg엔터테인먼트' and not sex='M') or (job_title = '가수' and not agency = 'yg엔터테인먼트');
select *
from celeb
where (birthday >19891231 and not sex='F') or (birthday < 19800101 and not agency='안테나');
01) Celeb 테이블에서 직업이 가수가 아니면서 성별이 여자이거나,
나이가 40보다 작지 않으면서 아이디가 홀수인 데이터를 조회하세요
select *
from celeb
where (not job_title = '가수' and sex = 'F')
or (not age < 40 and (id%2) = 1);
BETWEEN = 조건값이 범위 사이에 있으면 true 리턴
<기본 구조>
SELECT col1, col2
FROM tablename
WHERE col1 BETWEEN value1 AND value2;
🚨컬럼명 --> 검색값이 A와 B 사이에 있는 데이터 추출
select *
from celeb
where age BETWEEN 20 AND 40;
✍️ betweeen 아니어도 비교연산자 사용해서 결과 도출 가능함
ex) between 대신 -->where age>=20 and age<=40
select *
from celeb
where (not birthday between 19800101 and 19951231 and sex='F')
or (agency= 'yg엔터테인먼트' and not age between 20 and 45);
01) Celeb 테이블에서 아이디가 1 에서 5사이의 값이면서 성별이 여자이거나, 아이디가 홀수이면서 성별이 남자이면서 나이가 20세에서 30세 사이인 데이터를 조회하세요.
select *
from celeb
where (id between 1 and 5 and sex='F')
or ((id%2) = 1 and sex='M' and age between 20 and 30);
IN = 목록 안에 조건값이 존재하면 true 리턴
<기본 구조>
SELECT col1, col2
FROM tablename
WHERE column IN (value1, value2, ...);
🚨 컬럼명을 주고 --> IN ( 범위 )
'or' 연산자로도 결과 도출 가능!
select *
from celeb
where not agency in ('나무엑터스', '안테나', '울림엔터테인먼트') and
(sex ='F' or age >= 45);
🚨우선순위 주의!!
--> 소속사 조건 먼저 체크
--> 나이와 성별 체크를 한 후
--> 이 두 조건 그룹을 and로 묶어주기
01) celeb 테이블에서 아이유, 송강, 강동원, 차승원 중에 YG엔터테이먼트 소속이 아니거나 나이가 40세에서 50세 사이인 사람을 찾아보세요.
select *
from celeb
where name in ('아이유', '송강', '강동원', '차승원')
and (not agency='yg엔터테인먼트' or age between 40 and 50);
🚨괄호 주의!!🚨
LIKE = 조건값이 (문자열) 패턴에 맞으면 true 리턴
🚨 문자열!!
<기본 구조>
SELECT col1, col2
FROM tablename
WHERE column LIKE pattern;
🚨 컬럼명을 먼저 --> LIKE 패턴
select *
from celeb
where agency LIKE 'yg엔터테인먼트';
✍️ LIKE 없이도 결과 도출 가능!
ex) like 대신where agency='yg엔터테인먼트'
'%' : 아무 글자가 와도 상관 없음
select *
from celeb
where agency LIKE 'yg%';
select *
from celeb
where agency LIKE '%엔터테인먼트';
select *
from celeb
where job_title LIKE '%가수%';
✍️'%가수%'
'가수'라는 단어로 시작해도 끝나고 중간에 있어도 검색
언더바 '_' : 글자 수 확보해야 할 때,
select *
from celeb
where agency LIKE '_G%';
select * from celeb
where job_title LIKE '가_%';
✍️ 언더바 '_' 로 최소 글자 확보 후 %로 이상
select * from celeb
where job_title LIKE '영%모델';
select * from celeb
where job_title LIKE '%영화배우%' AND job_title LIKE '%탤런트%';