📝MySQL Workbench 데이터 가공
데이터를 유니크하게 만드는(중복제거) distinct
데이터를 정렬하는 order by
데이터를 끊어서 가져오는 limit 와 offset
문자열 안에 원하는 문자가 들어있는지 검색하는 like
데이터를 유니크하게 만드는(중복제거) distinct
-- author_lname은 카테고리컬 데이터다 유니크한 데이터를 확인하기
select distinct author_lname
from books;
-- 작가의 퍼스트네임과 라스트네임을 합친 full name으로 유니크하게 확인하기
select distinct concat(author_fname, ' ', author_lname) as 'full name'
from books;
데이터를 정렬하는 order by
※ 내림차순 정렬은 desc 사용 / 오름차순 정렬은 아무것도 안쓰거나 asc 사용
-- author_lname으로 정렬
select *
from books
order by author_lname;
-- author_fname과 author_lname을 합친 full name으로 정렬
select id, title, concat(author_fname, ' ', author_lname) as full_name,
released_year, stock_quantity, pages
from books
order by full_name;
-- author_lname으로 정렬하되 이름이 같으면 author_fname으로 정렬하기
select *
from books
order by author_lname, author_fname;
-- author_lname으로 정렬하되 author_lname은 내림차순, author_fname은 오름차순
select *
from books
order by author_lname desc, author_fname asc;
데이터를 끊어서 가져오는 limit 와 offset
※ limit 에 숫자 2개를 입력하면 왼쪽은 시작위치(offset) 오른쪽은 갯수
페이징(paging)에 사용된다
-- 테이블의 데이터를 5개만 가져오기
select *
from books
limit 5;
-- limit 에 숫자 2개를 입력하면 왼쪽은 시작위치(offset) 오른쪽은 갯수
select *
from books
limit 0, 5;
-- 위에서 가져온 5개 이후로 5개의 데이터를 가져오기
select *
from books
limit 5, 5;
-- 출간년도 내림차순으로 정렬하여 처음부터 7개의 데이터를 가져오기
select *
from books
order by released_year desc
limit 0, 7;
문자열 안에 원하는 문자가 들어있는지 검색하는 like
※ 대소문자 구별하지 않고 가져옴
-- 책 제목에 the가 들어가 있는 데이터를 가져오기
select *
from books
where title like '%the%';
-- 책 제목 시작이 the로 시작하는 데이터를 가져오기
select *
from books
where title like 'the%';
-- 'the%' = the로 시작하고 뒤에는 아무글자가 와도 상관없다
-- 책 제목이 the로 끝나는 데이터를 가져오기
select *
from books
where title like '%the';
-- '%the' = 앞에는 아무글자가 와도 상관없이 the로 끝나는 것
-- stock_quantity의 숫자가 두자리인 데이터만 가져오기 _언더스코어로 표시
select *
from books
where stock_quantity like '__';
★텍스트 내용이 많은 경우 성능이 떨어지고 과부하가 발생해 응답시간이 길어질 것이다
그러므로 검색기능을 사용할때는 검색 속도를 높이기 위해 검색 하려는 컬럼에 대해 풀텍스트(fulltext) 인덱스를 꼭 해준다
풀텍스는 텍스트 데이터를 빠르게 검색하기 위한 MySQL의 부가적인 기능
'MySQL' 카테고리의 다른 글
[MySQL] 데이터 그룹하여 처리하기 group by (0) | 2022.12.07 |
---|---|
[MySQL] 데이터 집계함수 count, sum, avg, max, min (0) | 2022.12.07 |
[MySQL] 문자열 컬럼의 데이터를 가공하는 함수들 concat(), substring(), replace(), reverse(), char_length(), upper(), lower() (0) | 2022.12.06 |
[MySQL] 테이블 데이터 CRUD(Create, Read, Update, Delete) (0) | 2022.12.06 |
[MySQL] id 컬럼 만들기 (0) | 2022.12.06 |
댓글