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 ;
'MySQL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
MySQL) ๋ ๊ฐ์ ํ ์ด๋ธ ์กฐ์ธํ๊ธฐ (2) | 2024.05.16 |
---|---|
MySQL) ๋ฐ์ดํฐ๋ฒ ์ด์ค DATE(๋ ์ง) ๊ฐ๊ณตํ๋ ํจ์ (0) | 2024.05.16 |
MySQL) Reverse, Char_length, Ifnull ํจ์ ์ฌ์ฉํ๊ธฐ (0) | 2024.05.14 |
MySQL) Substring, Replace, Upper/Lower ํจ์ ์ฌ์ฉํ๊ธฐ (0) | 2024.05.14 |
MySQL) ์๋ธ์ฟผ๋ฆฌ(Sub Query)์ Concat, Max, Min, Avg ํจ์ ์ฌ์ฉํ๊ธฐ (0) | 2024.05.14 |