데이터베이스/mysql

mysql-구분점을 기준으로 값 추출

nyeongha 2023. 12. 16. 22:41

email 도메인을 추출하고싶을떄 @를 기준으로 뒷부분을 가져오면된다.

mysql에는 이를 도와주는 함수가 있는데 substring_index이다.

사용법은 아래와 같다.

substring_index(컬럼명,'구분점',구분자 index)

예를 들어 email컬럼에서 도메인값을 가져오고싶다면 아래처럼 작성한다.

substring_index(email,'@',-1)

 

 

최종적으로 쿼리문을 작성하면

select 
email,
substring_index(email,'@',-1) AS email_domain,
from customers c
where email like '%@%'
group by email_domain

이와 같은 결과를 가져올 수 있다.

 

 

도메인 구분하기