๋ฐ์ดํฐ ๋ถ์ํ๊ธฐ
-- ์ ์ ์ค์์ ๊ฐ์ฅ ์ค๋๋ ํ์ ๋ค์ฏ๋ช ์ ์ฐพ์ผ์ธ์
select *
from users
order by created_at asc
limit 5;
-- ํ์๊ฐ์
์ ๊ฐ์ฅ ๋ง์ด ํ๋ ์์ผ์ ๋ฌด์จ์์ผ??
select dayname(created_at) as dayname, count(id) as count
from users
group by dayname
order by count desc;
-- ํ์๊ฐ์
์ ํ์ง๋ง, ์ฌ์ง์ ํ๋ฒ๋ ์ฌ๋ฆฌ์ง ์์ ์ ๋ นํ์๋ค์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค์์ค
select *
from users u
left join photos p
on u.id = p.user_id
where p.image_url is null ;
-- ๊ฐ์ฅ ์ ๋ช
ํ ์ฌ์ง์ ๋ฌด์์ธ์ง ์ฐพ์์ ๊ทธ ์ฌ์ง์ ์ ์ ์ด๋ฆ, ์ด๋ฏธ์ง์ฃผ์, ์ข์์ ์๋ฅผ ๋ํ๋ด์ธ์
select u.username, p.image_url , count(l.photo_id) as like_count
from photos p
join likes l
on p.id = l.photo_id
join users u
on p.user_id = u.id
group by l.photo_id
order by like_count desc
limit 1;
-- ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉ๋ ํด์ํ๊ทธ์ ์ด๋ฆ์ ๋ฌด์์ด๋ฉฐ, ๋ช๊ฐ๋ ์ฌ์ฉ๋์๋์ง ๋ํ๋ด์์ค
-- ์ฆ ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉ๋ ํด์ํ๊ทธ์ ์ด๋ฆ, ๊ฐฏ์๋ฅผ ํ์ํ์์ค
select t.tag_name, count(pt.tag_id) as tag_count
from photo_tags pt
join tags t
on pt.tag_id = t.id
group by tag_id
order by tag_count desc
limit 1;