[프로그래머스] 자동차 종류 별 특정 옵션이 포함된 자동차 수 구하기

yenpkr·2025년 2월 13일
0

sql

목록 보기
19/91

문제

제출

SELECT car_type,count(*) as CARS
from CAR_RENTAL_COMPANY_CAR
where options like '%통풍시트%' or options like '%열선시트%' or options like '%가죽시트%'
group by car_type
order by car_type asc

📌 새로 배운 내용

✅ REGEXP (정규표현식)

특정한 규칙을 가진 문자열의 집합을 표현하는데 사용하는 형식 언어이다.

문자열을 처리하는 방법 중의 하나로, 특정한 조건의 문자를 ‘검색’하거나 ‘치환’하는 과정을 매우 간편하게 처리할 수 있도록 해주는 수단이다.

정규 표현식은 Pattern을 사용해서 문자열을 처리한다.
패턴은 아래와 같다.

  • Matching

  • Numbers Limit

  • String Group

  • Not

  • 예시

# 통풍시트,열선시트,가죽시트가 포함된 문자열을 찾고 싶을 때

# 정규표현식 사용 안 했을 때
SELECT car_type,count(*) as CARS
from CAR_RENTAL_COMPANY_CAR
where options like '%통풍시트%' or options like '%열선시트%' or options like '%가죽시트%'

# 정규표현식 사용했을 때
SELECT car_type,count(*) as CARS
from CAR_RENTAL_COMPANY_CAR
where options REGEXP ('통풍시트|열선시트|가죽시트')

# 안녕하세요,반갑습니다로 시작하는 문자열을 찾고 싶을 때

# 정규표현식 사용 안 했을 때
SELECT car_type,count(*) as CARS
from CAR_RENTAL_COMPANY_CAR
where options like '안녕하세요%' or options like '반갑습니다%'

# 정규표현식 사용했을 때
SELECT car_type,count(*) as CARS
from CAR_RENTAL_COMPANY_CAR
where options REGEXP ('^안녕하세요|^반갑습니다')

또 다른 답

SELECT car_type,count(*) as CARS
from CAR_RENTAL_COMPANY_CAR
where options REGEXP ('통풍시트|열선시트|가죽시트')
group by car_type
order by car_type asc

정규표현식 참조 블로그

0개의 댓글