728x90

< 링크 >
여기를 클릭하세요 (프로그래머스 고득점 Kit 106문제 모음)

여기를 클릭하세요 (8. 3월에 태어난 여성 회원 목록 출력하기)

< 문제 >
MEMBER_PROFILE 테이블에서 생일이 3월인 여성 회원의 ID, 이름, 성별, 생년월일을 조회하는 SQL문을 작성해주세요. 이때 전화번호가 NULL인 경우는 출력대상에서 제외시켜 주시고, 결과는 회원ID를 기준으로 오름차순 정렬해주세요.

< 내가 작성한 코드 >
SELECT member_id, member_name, gender, date_format(date_of_birth, '%Y-%m-%d') as DATE_OF_BIRTH
from member_profile
where (month(date_of_birth) = 3) and (gender = 'W') and (tlno is not null);

< 풀이 >
1. ID, 이름, 성별, 생년월일을 조회하라고 했으니 기본 구문을 작성, 조회했다.
select member_id, member_name, gender, date_of_birth
from member_profile;

2. 조건이 3가지 있어서 where 구문을 추가해야 한다.
- 생일이 3월인 사람
- 여성
- null 값 제외
where (month(date_of_birth) = 3) and (gender = 'W') and (tlno is not null)
★ is null : null 값인 경우 조회
★ is not null : null 값이 아닌 경우 조회

3. 문제 마지막에 'DATE_OF_BIRTH의 데이트 포맷이 예시와 동일해야 정답처리 됩니다.' 는 부분이 있어서
select date_of_birth 를 date_format() 을 사용해서 출력 형식을 바꿔야 한다.
date_format()으로 검색하면 '%y-%m-%d' 에서
y, m, d가 대문자인지 소문자인지, 몇 글자인지에 따라 출력 내용이 다르다.

문제에서 주어진 내용처럼 출력하려면(1993-03-16 처럼 출력)
date_format(date_of_birth, '%Y-%m-%d')로 변경해야 한다.

< 내가 작성한 코드 >
SELECT member_id, member_name, gender, date_format(date_of_birth, '%Y-%m-%d') as DATE_OF_BIRTH
from member_profile
where (month(date_of_birth) = 3) and (gender = 'W') and (tlno is not null);

+ Recent posts