연산된 결과: view (가상 테이블)
1. 단일행 함수
날짜/시간
select now();

select date('2025-02-25 12:30:35');
select time('2025-02-25 12:30:35');
select year('2025-02-25 12:30:35');
select month('2025-02-25 12:30:35');
select day('2025-02-25 12:30:35');
select hour('2025-02-25 12:30:35');
select minute('2025-02-25 12:30:35');
select second('2025-02-25 12:30:35');








select '2025-02-25';

select ename, hiredate, now()
from emp;

날짜 포맷
select date_format(now(),'%y/%m/%d-%h:%i:%s');

날짜 연산하기_더하기
select date_add('2025-02-25', interval 4 year);
select date_add(now(), interval 4 month);
select date_add(now(), interval 4 week);
select date_add(now(), interval 4 day);
select date_add(now(), interval 4 hour);
select date_add(now(), interval 4 minute);
select date_add(now(), interval 4 second);







날짜 연산하기_빼기
select date_sub('2025-02-25', interval 4 year);
select date_sub('2025-02-25', interval 4 month);


날짜 연산하기_간격
select datediff('2025-02-25', '2025-03-01');
select timediff(now(), '2025-02-25 12:50:00');


날짜 연산하기_마지막날짜
select last_day(now());

수학 함수
select floor(101.5);
select ceil(101.5);
select round(101.4);
select mod(101,10);




문자열 함수_문자열 슬라이싱
-- substr(시작번지 1~, 갯수)
select substr(hiredate,1,4)
from emp;
select year(hiredate)
from emp;
select replace('010/2222/7777','/','-');



문자열 함수_문자열 인덱스 번호 찾기
select instr('abcde','c');
select rpad('ssarmango',10,'*');
select rpad(substr('ssarmango',1,4),length('ssarmango'),'*')
-- 남는 자리 * 표시
select lpad(substr('ssarmango',1,4),length('ssarmango'),'*');




문제_전화번호 중간자리만 * 표시
-- 샘플링 / 한개만 해보기
select instr('055)381-2158', ')'); -- 4 / 시작번호 4+1

select instr('055)381-2158', '-'); -- 8 / 마지막 번호 8-4-1

-- substr() 사용해서 중간번호만 출력
select substr('055)381-2158',instr('055)381-2158', ')')+1,instr('055)381-2158', '-')-instr('055)381-2158', ')')-1);

repeat() → 곱하기
*을 중간번호 자릿수만큼 곱해야함(길이_lenght)
-- 뽑은 중간자리를 * 표시되게 출력하기
select repeat('*',length(substr('055)381-2158',instr('055)381-2158', ')')+1,instr('055)381-2158', '-')-instr('055)381-2158', ')')-1)));

-- 번호 전체 중 중간자리만 * 표시되게 출력하기
select replace('055)381-2158',substr('055)381-2158',instr('055)381-2158', ')')+1,instr('055)381-2158', '-')-instr('055)381-2158', ')')-1),repeat('*',length(substr('055)381-2158',instr('055)381-2158', ')')+1,instr('055)381-2158', '-')-instr('055)381-2158', ')')-1))));

완성
select tel, replace(tel,substr(tel,instr(tel, ')')+1,instr(tel, '-')-instr(tel, ')')-1),repeat('*',length(substr(tel,instr(tel, ')')+1,instr(tel, '-')-instr(tel, ')')-1))));

2. 조건문
-- 조건문 (if - mySQL, case when - 모든 DB)
select if(10>5,"참","거짓");

-- 2500 (고액연봉), (일반연봉) // if보다 깔끔하고 가독성이 좋다
select ename, sal,
case
when sal>2500 then "고액연봉"
when sal<2000 then "일반연봉"
else "중간연봉"
end "연봉그룹" -- 별칭: 연봉그룹
from emp;

MySQL 날짜 포맷 키 값 목록
키 | 설명 | 예시 ( '2025-02-25 15:30:45' 기준) |
%Y | 4자리 연도 | 2025 |
%y | 2자리 연도 | 25 |
%m | 2자리 월(01~12) | 02 |
%c | 1 | 2 |
%M | 월 이름 (영어) | February |
%b | 월 이름 축약형 (영어) | Feb |
%d | 2자리 일(01~31) | 25 |
%e | 1 | 25 |
%W | 요일 (영어) | Tuesday |
%a | 요일 축약형 (영어) | Tue |
%w | 요일 숫자(0=일요일 ~ 6=토요일) | 2 |
%j | 연중 몇 번째 날인지(001~366) | 056 |
%H | 24시간제 시간 (00~23) | 15 |
%h 또는 %I | 12시간제 시간 (01~12) | 03 |
%p | AM/PM 표시 | PM |
%i | 분 (00~59) | 30 |
%s | 초 (00~59) | 45 |
%f | 마이크로초 (6자리) | 000000 |
%r | 12시간제 시간 + AM/PM ( %I:%i:%s %p ) | 03:30:45 PM |
%T | 24시간제 시간 ( %H:%i:%s ) | 15:30:45 |
%X | 주의 연도 (ISO 8601, 4자리) | 2025 |
%x | 주의 연도 (2자리) | 25 |
%v | 주차 (ISO 8601, 01~53) | 09 |
%U | 주차 (일요일 시작, 00~53) | 08 |
%u | 주차 (월요일 시작, 01~53) | 09 |
%D | 일 + 서수 접미사 ( 1st, 2nd, 3rd... ) | 25th |
기본적인 수학함수
함수 | 설명 | 예제 | 결과 |
ABS(x) | 절댓값 반환 | SELECT ABS(-10); | 10 |
CEIL(x) | 올림 (큰 정수) | SELECT CEIL(101.5); | 102 |
FLOOR(x) | 내림 (작은 정수) | SELECT FLOOR(101.5); | 101 |
ROUND(x) | 반올림 | SELECT ROUND(101.4); | 101 |
ROUND(x, d) | 소수점 d 자리까지 반올림 | SELECT ROUND(101.456, 2); | 101.46 |
TRUNCATE(x, d) | 소수점 d 자리까지 절삭(버림) | SELECT TRUNCATE(101.456, 2); | 101.45 |
MOD(x, y) | x를 y로 나눈 나머지 | SELECT MOD(101, 10); | 1 |
Share article