DB/명령프롬프트 - mysql 예시

[MySQL] SQL 내장함수 예시

congs 2023. 3. 28. 17:25

1. addr의 시를 특별시로 변경하세요 (replace)

# 서울시에서 '시'를 '특별시'로 바꾸기

select replace ('서울시','시','특별시');

 

# test3의 addr의 '시'를 '특별시'로 바꾸기

update test3 set addr = replace(addr,'시','특별시');

 

#select로 확인해보고 설정하기 = 설정이 바뀌지 않기 때문!

select replace(addr,'시','특별시') from test3;

 

2. dep의 학과가 아닌 값들 뒤에 --학과로 만들기 (concat,right)

1) 학과가 있는 값을 찾아서 제외하고 붙이기

 

2) 학과가 없는 값을 찾아 붙이기 

(뒤에 두 글자가 학과가 아닌경우, 학과를 concat)

update test3 set dep = concat(dep,'학과')

where right(dep,2) <>학과' ;

 

3. tel이 null인 자료만 검색  (is null / is not null)

(null은 값이 없는 자료로 검색이 불가능)

( is null : null일 경우 / is not null : null이 아닐 경우 )

select * from test3 where tel is null;

 

4. tel이 null이면 '미등록'으로 설정 (설정 값의 변경 modify)

(tel의 값을 int로 두었기때문에 자료형 변경이 필요)

alter table test3 modify column tel varchar(10);

 

update test3 set tel = '미등록'

where tel is null;