코딩일기
SQL 코드카타 121 - Patients With a Condition
서재일
2024. 7. 14. 13:17
https://leetcode.com/problems/patients-with-a-condition/description/
Column Name | Type |
patient_id | int |
patient_name | varchar |
conditions | varchar |
Patients 테이블:
- patient_id : 테이블의 고유 키
- patient_name : 환자의 이름
- conditions : 환자의 상태
Type I Diabetes (제1형 당뇨병)을 가진 환자들의 patient_id, patient_name, conditions을 찾으세요. Type I Diabetes는 항상 DIAB1 접두사로 시작합니다.
SELECT patient_id, patient_name, conditions
FROM Patients
WHERE conditions REGEXP '\\bDIAB1';
REGEXP(정규표현식)을 사용해서 문제를 해결했다. 처음에는 like를 사용하려고 했지만 생각보다 예외상황이 많았고 다른 방법을 사용하기로 했다.
REGEXP '\\bDIAB1'는 'DIAB1'이라는 단어로 시작하는 패턴을 찾아낼 수 있다.