본문 바로가기
AWS/MySQL

MySQL 유니크,정렬,n개만가져오기, 문자열안에 원하는 문자를 검색, 자리수로 가져오기

by leopard4 2022. 12. 7.

연습할 테이블의 모습

-- 연습용 테이블 생성을 위한 코드
INSERT INTO books (title, author_fname, author_lname, released_year, stock_quantity, pages)
VALUES
('The Namesake', 'Jhumpa', 'Lahiri', 2003, 32, 291),
('Norse Mythology', 'Neil', 'Gaiman',2016, 43, 304),
('American Gods', 'Neil', 'Gaiman', 2001, 12, 465),
('Interpreter of Maladies', 'Jhumpa', 'Lahiri', 1996, 97, 198),
('A Hologram for the King: A Novel', 'Dave', 'Eggers', 2012, 154, 352),
('The Circle', 'Dave', 'Eggers', 2013, 26, 504),
('The Amazing Adventures of Kavalier & Clay', 'Michael', 'Chabon', 2000, 68, 634),
('Just Kids', 'Patti', 'Smith', 2010, 55, 304),
('A Heartbreaking Work of Staggering Genius', 'Dave', 'Eggers', 2001, 104, 437),
('Coraline', 'Neil', 'Gaiman', 2003, 100, 208),
('What We Talk About When We Talk About Love: Stories', 'Raymond', 'Carver', 1981, 23, 176),
("Where I'm Calling From: Selected Stories", 'Raymond', 'Carver', 1989, 12, 526),
('White Noise', 'Don', 'DeLillo', 1985, 49, 320),
('Cannery Row', 'John', 'Steinbeck', 1945, 95, 181),
('Oblivion: Stories', 'David', 'Foster Wallace', 2004, 172, 329),
('Consider the Lobster', 'David', 'Foster Wallace', 2005, 92, 343);
----- 새로운 데이터 3개 를 추가
insert into books
(title, author_fname, author_lname, released_year, stock_quantity,
pages)
values
('10% Happier', 'Dan', 'Harris', 2014, 29, 256),
('fake_book', 'Freida', 'Harris', 2001, 287, 428),
('Lincoln In The Bardo', 'George', 'Saunders', 2017, 111, 388);

-- 데이터를 유니크하게 만드는 키워드 distinct 
-- author_lname 은 카테고리컬 데이터다. 유니크한 데이터를 확인하자. (중복제거)
select distinct author_lname
from books;

-- 작가의 퍼스트네임과 라스트네임을 합친, full name 으로 확인하자.
select distinct concat(author_lname," ", author_fname) as 'full name unique'
from books;

-- 정렬하는 방법 : order by 키워드
-- author_lname 으로 정렬
select * 
from books
order by author_lname ; 

-- full_name 으로 정렬 (SQL 동작 방식을 이해)
-- 정렬은 select from 이 진행된 후 마지막에 실행된다.
select id, title, concat(author_lname," ", author_fname) as full_name, released_year, stock_quantity, pages
from books
order by concat(author_lname," ", author_fname);

-- full_name 내림차순으로 정렬 ( desc ) , 오름차순은 ( 아무것도 쓰지않거나 asc )
select id, title, concat(author_lname," ", author_fname) as full_name, released_year, stock_quantity, pages
from books
order by full_name desc;

-- 출간년도로 내림차순 정렬하여, 책 제목, 출간년도, 페이지수를 가져오시오
select title, released_year, pages
from books
order by released_year desc ;

-- author_fname 으로 정렬하되, 이름이 같으면 author_lname 으로 정렬하시오.
select author_lname, author_fname
from books
order by author_lname , author_fname;

-- author_lname 으로 정렬하되,  author_lname은 내림차순,  
-- author_fname 은 오름차순으로 정렬하시오.
select  author_lname,  author_fname
from books
order by  author_lname desc,   author_fname asc;

-- 데이터를 끊어서 가져오는 방법 limit 와 offset
-- 테이블의 데이터를 5개만 가져오시오.
select *
from books
limit 5 ; 
-- limit 에 숫자가 2개 나오면, 왼쪽은 시작위치(offset), 오른쪽은 갯수
select *
from books
limit 0,5;   -- 0 은 컴퓨터가 매기는 인덱스

-- 위에서 가져온 5개 이후로, 5개의 데이터를 가져오시오.
select *
from books
limit 5,5;
-- 위에서 가져온 5개 이후로, 5개의 데이터를 가져오시오.
select *
from books
limit 10,5;
-- 위에서 가져온 5개 이후로, 5개의 데이터를 가져오시오.
select *
from books
limit 15,5;

-- 출간년도 내림차순으로 정렬하여, 처음부터 7개의 데이터를 가져오시오.
select *
from books
order by released_year desc
limit 0,7;

-- 출간년도 내림차순으로 정렬하여, 7개부터 7개의 데이터를 가져오시오.
select *
from books
order by released_year desc
limit 7,7;

-- 문자열 안에 원하는 문자가 들어있는지 검색 : like 키워드 [참고 : 상위버전인 엘라스틱 서치가 있다]
-- 책 제목에 the 가 들어가 있는 데이터를 가져오시오.
select * 
from books
where title like '%the%' ;    -- 대소문자 구분없이 가져온다, %%는 앞뒤로 아무글자가 와도 상관없다는의미
-- 책 제목에 시작이 the 로 시작하는 데이터를 가져오시오.
select *
from books
where title like 'the%';
-- 책 제목이, the 로 끝나는 데이터를 가져오시오.
select *
from books
where title like '%the';

-- stock_quantity 의 숫자가 두자리인 데이터만 가져오시오. _ 언더스코어로 표시.
select * 
from books 
where stock_quantity like '__';       -- 언더스코어가 2개 이므로 2자리수를 가져온다. 문자열도 가능


-- pages 수는 100보다 크고, 책 제목에 the 가 들어간 책을 가져오되, 
-- 재고수량 내림차순으로 데이터를 3개만 가져오시오.
select *
from books
where pages > 100 and title like '%the%'
order by stock_quantity desc
limit 0,3 ;