본문 바로가기
AWS/MySQL

MySQL !=, group by having, between A and B, not like, case, if ,not in, not null

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);

 

-- 데이터를 가져오는 방법의 여러 문법들 설명.

-- not equal ( 그게 아닌거 )
-- 출간 년도가 2017 년도가 아닌 데이터를 가져오시오.

select *
from books
where released_year != 2017;

-- 작가의 author_lname 이 'Harris' 가 아닌 데이터에서, 책제목과 페이지수를 가져오시오.
select title, pages
from books
where author_lname != 'Harris';

-- not like ( 그거 포함하지 않은거 )
-- 책 제목에 W 가 포함되지않은 책을 가져오시오.
select *
from books
where title not like '%w%' ;

-- 책의 재고가 100이상인 데이터에서, 책제목과 재고만 가져오세요.
select title, stock_quantity
from books
where stock_quantity >= 100;

-- 출간년도가 2000년 이상인 책에 대해서 최신책으로 정렬하여, 제목과 년도를 보여주세요.
select title, released_year
from books
where released_year >= 2000
order by released_year desc;

-- 출간년도가 1990년에서 2015년 사이의 책의 데이터에서, 제목과 년도를 보여주세요.
select title,released_year
from books
where released_year >= 1990 and released_year <= 2015;

-- 책 재고가 100보다 크거나 30보다 작은 책들만 가져오세요.
select *
from books
where stock_quantity >= 100 or stock_quantity <=30;

-- author_lname 이 Eggers 이거나 출간년도가 2010년 이상인 책 데이터를 가져오시오.
select *
from books
where author_lname = 'Eggers' or released_year >= 2010;

-- 출간년도가 1990년에서 2015년 사이의 책의 데이터에서, 제목과 년도를 보여주세요.
-- between A and B
select *
from books 
where released_year between 1990 and 2015 ; 

-- 년도별 stock_quantity 의 평균값이 70보다 큰 책들의, 년도와 평균값을 보여주세요.
select released_year, avg(stock_quantity) as average
from books
group by released_year having average > 70 ;

-- 출간년도가 2002년, 2004년, 2006년, 2012년, 2015년 출간된 책들의 데이터만
-- 가져오시오. 파이썬과 다르게 mySQL은 [] 대신 ()를 쓴다.
select *
from books
where released_year in (2002, 2004, 2006, 2012, 2015);

-- 출간년도가 2002년, 2004년, 2006년, 2012년, 2015년이 아닌 출간된 책들의 데이터만
-- 가져오시오.
select *
from books
where released_year not in (2002, 2004, 2006, 2012, 2015);


-- null 인 데이터를 가져오는 방법 : is null
-- comments 테이블에서 updated_at 컬럼이 null 인 데이터를 가져오시오.
select *
from comments 
where updated_at is null;

-- comments 테이블에서 updated_at 컬럼이 null 이 아닌 데이터를 가져오시오.
select *
from comments 
where updated_at is not null;

-- 년도가 2000년 이상이면, 'Mordern Book' 이라고 하고,alter
-- 그렇지 않으면, '20th Book' 이라고, 새로운 컬럼 Genre 컬럼을 만드세요.
-- 1. case 문으로 처리하는 방법
select * ,
	case 
		when released_year >= 2000 then 'Mordern Book'
        else '20th Book'
    end as genre
from books ; 

-- 2. if 함수로 처리하는 방법
select * , if(released_year >= 2000 , 'Mordern Book', '20th Book') as genre
from books;

-- 재고가 0~50이면, *
-- 51~100 이면, **
-- 101~150 이면, ***
-- 그렇지 않으면, ****    로 표시하는 컬럼 stock 이라는 컬럼을 만드세요
select *,
	case
		when stock_quantity between 0 and 50 then '*'
		when stock_quantity between 51 and 100 then '**'
		when stock_quantity between 101 and 150 then '***'
        else '****'
        end as stock
from books;