MySQL

[MySQL] 문자열 검색에 유용한 like

건휘맨 2024. 5. 14. 11:12

like : 문자열 안에 원하는 문자가 들어있는지 검색

-- 책 제목에 the 가 들어있는 데이터를 가져오시오.
-- 책 제목이 the 인거 가져오시오. 와는 완 전 히 다른것!!

select *
from books
where title = 'the';

select *
from books
where title like '%the%';

--  %는 "the" 앞, 뒤, 또는 둘 다의 어떤 문자열이든 올 수 있다는 것을 의미

 

 

-- 책 제목에 시작이 the 로 시작하는 데이터를 가져오시오.

select *
from books
where title like 'the%';

 

-- stock_quantity 의 숫자가, 두 자리인 데이터만 가져오시오.
-- 언더스코어 사용! __

select *
from books
where stock_quantity like '__';

-- __ 는 두 개의 문자를 가진 문자열을 검색

 

-- pages 수는 100보다 크고 책 제목에 the 가 들어간 책을 가져오되
-- 재고수량 내림차순으로 데이터를 3개만 가져오시오.

select *
from books
where pages > 100 and title like '%the%'
order by stock_quantity desc
limit 3;

 

 

-- 책 제목에, talk 가 없는 데이터만 가져오시오.

select *
from books
where title not like '%talk%';