Programmers/SQL

[mysql/oracle] 자동차 종류 별 특정 옵션이 포함된 자동차 수 구하기

Anoi 2023. 1. 29. 15:16

문제

 

결과

 

mysql / oracle

1
2
3
4
5
SELECT car_type, count(*) cars
from car_rental_company_car
where options like '%시트%' 
group by car_type
order by car_type
cs

공교롭게도 요구한 통풍시트, 열선시트, 가죽시트 모두 '시트'가 들어가서 like와 와일드카드로 처리했다.

 

정규식으로도 가능하다.

1
2
3
4
5
SELECT car_type, count(*) cars
from car_rental_company_car
where options regexp '시트' 
group by car_type
order by car_type
cs
정규식 검색 

. : 문자 하나
* : 앞에 나온 문자의 0개 이상 반복
^ : 문자열의 가장 처음
$ : 문자열의 맨 끝
[.] : 괄호 안의 문자열 일치를 확인
{.} : 반복
| : or 
where name regexp '가|나|다|라'
where name regexp '[가-힇]' 한글 포함된 레코드 검색
where name regexp '^[가-힇]+$' 한글로만 된 것 검색