์ฅ๊ณ , ํ์ด์ฌ์ ์ธ ๋์๋ ๋ง์ง๋ง ์์์ ๋ํด ์ฝค๋ง๋ฅผ ๋ถ์ด๋ ๊ฒ์ด ๋ฌธ์ ์์ง๋ง sql์์๋ ์๋ฌ๊ฐ ๋ฌ๋ค.
์๋ฅผ ๋ค์ด urls.py ์์ endpoint๋ฅผ ์ ์ํ ๋, ๋ค์ ์ถ๊ฐํ ๊ฒ์ ์ผ๋ํด์ ์์์ ๋ง์ง๋ง์ ,๋ฅผ ๋ถ์ฌ๋ ๋ฌด๊ดํ๋ค.
urlpatterns = [ A, B, C, ]
ํ์ง๋ง raw sql์์ ๋ง์ง๋ง์ ,๋ฅผ ๋ถ์ด๋ฉด ์๋ฌ๊ฐ ๋ฌ๋ค.
INSERT INTO users ( email, password, ) values ('yeni@gg.com', 1234,) #์๋จ
INSERT INTO users ( email, password ) values ('yeni@gg.com', 1234) # ๋จ!
๊ฐ์ฒด ๋ช ์ ๋๋ธ์ฟผํธ, ๋ฌธ์์ด์ ์ฑ๊ธ์ฟผํธ๋ก ๊ฐ์ผ๋ค. ๊ทธ๋์ select ๊ฐ์ ์์ฝ์ด๋ ๋ณดํต ์ด๋ฆ์ผ๋ก ์ง์ ํ ์ ์์ง๋ง ๋๋ธ ์ฟผํธ๋ก ๊ฐ์ผ๋ค๋ฉด ๊ฐ๋ฅํ๋ค. (๋ฌผ๋ก ์ ์ฐ๋๊ฒ ์ ์ผ ์ข์ง๋ง)
select price * quantity as select from table; #์๋จ
select price * quantity as โselectโ from table; #๋จ!
mysql์์ ์ซ์๋ง์ผ๋ก ์ง์ ๋๋ ๊ฐ์ฒด๋ ํ์ฉ๋์ง ์๋๋ค. ์ค๋ผํด์ ๊ฒฝ์ฐ ์์ ์ซ์๋ก ์์ํ๋ ๊ฐ์ฒด๋ช ์ด ํ์ฉ๋์ง ์๋๋ค.
select ๊ตฌ์์ ์ ์ํ ๋ณ๋ช ์ where๊ตฌ์์ ์ฌ์ฉํ ์ ์๋ค. ์๋ํ๋ฉด ๋ฐ์ดํฐ๋ฒ ์ด์ค ์๋ฒ ๋ด์์ ๋ค์๊ณผ ๊ฐ์ ์์๋ก ์ฒ๋ฆฌ๋๊ธฐ ๋๋ฌธ์ด๋ค.
where ๊ตฌ(ํ) -> select ๊ตฌ(์ด) -> order by๊ตฌ
where ๊ตฌ๋ก ํ ์กฐ๊ฑด ์ผ์น ์ฌ๋ถ๋ฅผ ์กฐ์ฌํ ๋ค์ select๊ตฌ์์ ์ง์ ๋ ์ด์ ์ ํํด ๊ฒฐ๊ณผ๋ก ๋ฐํํ๊ธฐ ๋๋ฌธ์, where๊ตฌ๋ ๋ณ๋ช ์ ์ ์ ์๋ค.
select price * quantity as โamountโ from table
where price * quantity >= 2000; #๋จ
where amount >= 2000; # ์๋จ
order by amount desc; # ๋จ
๋ค๋ง order by๋ select ๊ตฌ์์ ๋ณ๋ช ์ ์ง์ ํ ๋ค์ ๋ณ๋ช ์ ์ฌ์ฉํด๋ ๋๋ค.
null๊ฐ์ ์ฐ์ฐ ๊ฒฐ๊ณผ๋ ๋ชจ๋ null์ด๋ค. null + 1์ 1์ด ์๋๋ผ๋ ๋ป์ด๋ค. ๋๋ ๋ทฐ๋ฅผ ์งค ๋ null ๊ฐ์ธ ์ํ์์ ์ฐ์ฐ์ ํ๋ ค๋ค๊ฐ ์๋ฌ๋ฅผ ๋ง๋ ์ ์ด ์๋ค. ๊ทธ๋์ 0์ผ๋ก ์ฐ์ ๋ ์ฝ๋๋ฅผ ๋ง๋ค์ด์ค ๋ค์ ์ฐ์ฐ์ ํ๋๋ก ์ค์ ํ๋ค.
null์ 0์ด ์๋๋ผ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์์ ๊ฐ์ด ๋ค์ด๊ฐ์ง ์์ null๋ก ์ฒ๋ฆฌ๋ record๋ฅผ ๋งํ๋ค. null๊ฐ์ ๊ฒ์ํ ๋์๋ ์๋์ ๊ฐ์ด ์ ๋ ฅํ๋ค. where๊ตฌ๋ก ์ง์ ํ๋ ์กฐ๊ฑด์์ ์ฐธ๊ณผ ๊ฑฐ์ง์ ๋ฐํํ๋ ๋น๊ต ์ฐ์ฐ์(=, <>, >, <, >=, <=)๋ ์ ์ด๋ฅผ ์ฌ์ฉํด์ ํ๊ธฐํ ์ ์๋ค.
is null
๋๋ is not null
select * from users where bank_account is null
select * from users where bank_account is not null
order by๋ฅผ ์ธ ๊ฒฝ์ฐ ์ ๋ ฌ์ desc(descendant, ๋ด๋ฆผ์ฐจ์) & asc(ascendant, ์ค๋ฆ์ฐจ์) ํค์๋๋ฅผ ๋ถ์ฌ์ ํ์ฉํ๋ค. ์์นํ๊ณผ ๋ฌธ์์ดํ ๋ฐ์ดํฐ๋ ๋์๊ด๊ณ์ ๊ณ์ฐ๋ฒ์ด ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ ๊ทธ ๋ถ๋ถ์ ์ฃผ์ํด์ผ ํ๋ค. ๋ฌธ์์ด ๋ฐ์ดํฐ๋ ์ฌ์ ์ ์์
์ ์ํด ๊ฒฐ์ ๋๋ค,