MySQL

MySQL) LIKE, CASE, IF, GROUP BY ๊ตฌ๋ฌธ ์‚ฌ์šฉํ•˜๊ธฐ

567Rabbit 2024. 5. 14. 13:31

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


-- pages ์ˆ˜๋Š” 100๋ณด๋‹ค ํฌ๊ณ , ์ฑ… ์ œ๋ชฉ์— the๊ฐ€ ๋“ค์–ด๊ฐ„ ์ฑ…์„ ๊ฐ€์ ธ์˜ค๋˜ ์žฌ๊ณ ์ˆ˜๋Ÿ‰ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ 3๊ฐœ๋งŒ ๊ฐ€์ ธ์˜ค์‹œ์˜ค
select *
from books
where pages > 100 and title like '%the%'
order by stock_quantity desc 
limit 0,3;


-- ์ฑ… ์ œ๋ชฉ์—, talk๊ฐ€ ์—†๋Š” ๋ฐ์ดํ„ฐ๋งŒ ๊ฐ€์ ธ์˜ค์‹œ์˜ค
select *
from books
where title not like '%talk%';


-- ์ œ๋ชฉ์— stories๊ฐ€ ํฌํ•จ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๋ชฉ๋งŒ ์กฐํšŒํ•˜์‹œ์˜ค
select title
from books
where title like '%stories%';


-- author_lname์— ๊ณต๋ฐฑ์ด ๋“ค์–ด์žˆ๋Š” ์‚ฌ๋žŒ์˜ ์ฑ… ์ œ๋ชฉ๊ณผ author_lname์„ ์กฐํšŒ
select title, author_lname
from books
where author_lname like '% %';


-- stock_qauntity์˜ ์ˆซ์ž๊ฐ€ ๋‘์ž๋ฆฌ์ธ ๋ฐ์ดํ„ฐ๋งŒ ๊ฐ€์ ธ์˜ค์‹œ์˜ค
-- ์–ธ๋”์Šค์ฝ”์–ด ์‚ฌ์šฉ! _ ***
select *
from books
where stock_quantity like '__';   #( _ _ ๋‘์ž๋ฆฌ๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.)

 

 

 

Case

 

-- ์ œ๋ชฉ์— stories๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์œผ๋ฉด Short Stories๋กœ ์ œ๋ชฉ์ด Just Kids์™€ ์ผ์น˜ํ•˜๊ฑฐ๋‚˜ ์ œ๋ชฉ์— Heartbreaking์ด ํฌํ•จ๋˜์–ด์žˆ์œผ๋ฉด Memoir๋กœ, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด Novel์ด๋ผ๊ณ  ํ‘œํ˜„ํ•˜์ž.
select title, author_lname,
case
when title like '%stories%' then 'Shoort Stories'
when title = 'Just Kids' or title like '%Heartbreaking%' then 'Memoir'
else 'Novel'
end as 'TYPE'
from books;


-- ์ถœํŒ๋…„๋„๊ฐ€ 2000๋…„ ์ด์ƒ์ธ ์ฑ…๋“ค์€ '์ตœ์‹ ์ฑ…'์ด๋ผ๊ณ  ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์€ ์ฑ…๋“ค์€ '์˜ˆ์ „์ฑ…'์ด๋ผ๊ณ  ํ•˜์—ฌ, type ์ปฌ๋Ÿผ์„ ๋งŒ๋“ค์ž
select * , 
Case when released_year >= 2000 then '์ตœ์‹ ์ฑ…'
else '์˜ˆ์ „์ฑ…'
end as 'type'
from books
order by released_year;


-- ์žฌ๊ณ ๊ฐ€ 0 ์ด์ƒ์ด๊ณ  50 ์ดํ•˜์ด๋ฉด *, 51 ์ด์ƒ์ด๊ณ  100 ์ดํ•˜์ด๋ฉด **, ์ด๋„์ €๋„ ์•„๋‹ˆ๋ฉด, ***๋กœ ํ•˜์—ฌ stock์ด๋ผ๋Š” ์ปฌ๋Ÿผ์„ ๋งŒ๋“ค์ž
select * , 
Case 
when stock_quantity between 0 and 50 then '*'
when stock_quantity between 51 and 100 then '**'
else '***'
end as stock
from books;

 

 

 

If

 

-- ์ถœํŒ๋…„๋„๊ฐ€ 2000๋…„ ์ด์ƒ์ธ ์ฑ…๋“ค์€ '์ตœ์‹ ์ฑ…'์ด๋ผ๊ณ  ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์€ ์ฑ…๋“ค์€ '์˜ˆ์ „์ฑ…'์ด๋ผ๊ณ  ํ•˜์—ฌ, type ์ปฌ๋Ÿผ์„ ๋งŒ๋“ค์ž
select *,
if(released_year >= 2000, '์ตœ์‹ ์ฑ…', '์˜ˆ์ „์ฑ…') as type
from books;



-- pages ์ปฌ๋Ÿผ์˜ ๊ฐ’์ด 300๋ณด๋‹ค ํฌ๋ฉด '๊ธด ์ฑ…'์ด๋ผ๊ณ  ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด '์งง์€์ฑ…'์ด๋ผ๊ณ  ํ•˜์—ฌ ์ƒˆ๋กœ์šด ์ปฌ๋Ÿผ book_type์„ ๋งŒ๋“ค์ž
select * ,
if(pages > 300, '๊ธด์ฑ…', '์งง์€์ฑ…' )
from books;

 

 


Group By

: ~๋ณ„๋กœ ๋ฌถ์–ด์„œ ์ฒ˜๋ฆฌํ•˜์ž

 

# having์€ group by์˜ ์กฐ๊ฑด์ ˆ๋กœ, group by ๋’ค์—๋Š” where์ด ์˜ฌ ์ˆ˜ ์—†๊ณ  having์œผ๋กœ ์กฐ๊ฑด์ ˆ์„ ์จ์•ผ ํ•œ๋‹ค

-- author_lname๋ณ„๋กœ ๋ช‡๊ถŒ์˜ ์ฑ…์„ ์ผ๋Š”์ง€ ๊ฐฏ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด์„ธ์š”
select title, author_lname, count(author_lname) as count
from books
group by author_lname;


-- ๋…„๋„๋ณ„ stock_quantity์˜ ํ‰๊ท ๊ฐ’์ด 70๋ณด๋‹ค ํฐ ์ฑ…๋“ค์˜ ๋…„๋„์™€ ํ‰๊ท ๊ฐ’?
select released_year, avg( stock_quantity ) as avg_stock
from books 
group by released_year having avg_stock >= 70 ;