Search
๐Ÿ“Š

MySQL Intermediate Syntax

Created at
2018/07/30
Updated at
2021/09/20
Tags
Keywords
Database
3 more properties
Table of Contents

1. ์ˆซ์ž ์˜ฌ๋ฆผ, ๋ฐ˜์˜ฌ๋ฆผ, ๋‚ด๋ฆผ

1) ์˜ฌ๋ฆผ
โ€ข
syntax: ceil(์ˆซ์ž)
โ€ข
์ž๋ฆฟ์ˆ˜๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ž˜์™€ ๊ฐ™์€ trick์ด ํ•„์š”
select ceil(12.345) -- ์˜ฌ๋ฆผ ์ž๋ฆฟ์ˆ˜ ์ง€์ • select ceil(12.345*10)*0.1 -- ์˜ฌ๋ฆผ ํ™œ์šฉ use world select percentage, ceil(percentage) from countrylanguage
SQL
๋ณต์‚ฌ
2) ๋ฐ˜์˜ฌ๋ฆผ
โ€ข
syntax: round(์ˆซ์ž, ๊ฒฐ๊ณผ ์†Œ์ˆ˜์  ์ž๋ฆฟ์ˆ˜)
select round(12.345, 2) select percentage, round(percentage,0) from countrylanguage
SQL
๋ณต์‚ฌ
3) ๋‚ด๋ฆผ
โ€ข
syntax: truncate(์ˆซ์ž, ๊ฒฐ๊ณผ ์†Œ์ˆ˜์  ์ž๋ฆฟ์ˆ˜)
โ€ข
์ฐธ๊ณ : truncate์€ table์„ ์‚ญ์ œํ•  ๋•Œ๋„ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, delete table๊ณผ ๋‹ฌ๋ฆฌ truncate table์€ ์•„์˜ˆ ํ…Œ์ด๋ธ”์„ ์ดˆ๊ธฐํ™”์‹œํ‚ด
select truncate(12.345, 1) select percentage, truncate(percentage, 1) from countrylanguage
SQL
๋ณต์‚ฌ

2. ์กฐ๊ฑด๋ฌธ

1) if
โ€ข
syntax: if(์กฐ๊ฑด, ์ฐธ์ธ ๊ฒฝ์šฐ, ๊ฑฐ์ง“์ธ ๊ฒฝ์šฐ)
-- ๋„์‹œ์˜ ์ธ๊ตฌ๊ฐ€ 100๋งŒ์ด ๋„˜์œผ๋ฉด big city, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด small city๋ฅผ scale ์ปฌ๋Ÿผ์— ์ถœ๋ ฅ select name, population, if(population >= 1000000, "big city", "small city") as scale from city having scale = "small city" order by population desc
SQL
๋ณต์‚ฌ
2) ifnull
โ€ข
syntax: ifnull(์ปฌ๋Ÿผ์ด๋ฆ„, ๋””ํดํŠธ ๋ฐ์ดํ„ฐ)
-- ๋…๋ฆฝ์—ฐ๋„๊ฐ€ ์—†๋Š” ๋ฐ์ดํ„ฐ๋ฅผ 0์œผ๋กœ ์ถœ๋ ฅ select indepyear, ifnull(indepyear, 0) from country
SQL
๋ณต์‚ฌ
3) case when then else end
โ€ข
python์˜ if๋ฌธ๊ณผ ๋น„์Šทํ•˜๊ฒŒ ์‚ฌ์šฉ
โ€ข
syntax: case when ์กฐ๊ฑด then ๊ฒฐ๊ณผ else ๊ฒฐ๊ณผ end
โ—ฆ
when, then์€ ๋ฐ˜๋ณต ๊ฐ€๋Šฅ
-- ๋‚˜๋ผ๋ณ„๋กœ ์ธ๊ตฌ 10์–ต ์ด์ƒ/1์–ต~10์–ต/1์–ต ์ดํ•˜๋ฅผ "big, medium, small"๋กœ ํ‘œ์‹œ select name, population, case when population > 1000000000 then "big" when population > 100000000 then "medium" else "small" end as scale from country having scale = "medium" order by population desc
SQL
๋ณต์‚ฌ

3. Date Format

use sakila select date_format(payment_date, "%Y-%m") as monthly, sum(amount) from payment group by monthly
SQL
๋ณต์‚ฌ

4. Join

-- join ์‹ค์Šต์— ์“ธ test1 database์™€ user table, addr table ์ƒ์„ฑ use test1 CREATE TABLE user ( user_id int(11) unsigned NOT NULL AUTO_INCREMENT, name varchar(30) DEFAULT NULL, PRIMARY KEY (user_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE addr ( id int(11) unsigned NOT NULL AUTO_INCREMENT, addr varchar(30) DEFAULT NULL, user_id int(11) DEFAULT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO user(name) VALUES ("jane"), ("po"), ("alice"), ("peter"); INSERT INTO addr(addr, user_id) VALUES ("seoul", 1), ("pusan", 2), ("deajeon", 3), ("deagu", 5), ("seoul", 6);
SQL
๋ณต์‚ฌ
1) Inner Join
โ€ข
๊ทธ๋ƒฅ inner join์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ (ํ‚ค ๋งค์นญ ์—†์ด)
โ—ฆ
user์˜ 5๊ฐœ ๋ฐ์ดํ„ฐ์— addr์˜ 4๊ฐœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ๊ฐ mappingํ•ด 20 row์˜ ๋ฐ์ดํ„ฐ ์ƒ์„ฑ
โ€ข
on์— ๋งค์นญ์‹œํ‚ฌ ์ปฌ๋Ÿผ์„ ์ง€์ •ํ•  ๊ฒฝ์šฐ
โ—ฆ
์ผ์น˜ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋งŒ ์ฐพ์•„์„œ join (๋‘ ํ…Œ์ด๋ธ”์— ๋‹ค ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋งŒ join)
select * from user inner join addr select * from user inner join addr on user.user_id = addr.user_id
SQL
๋ณต์‚ฌ
2) Left Join
โ€ข
user์— ์žˆ๋Š” user_id์—๋งŒ ์ฃผ์†Œ๋ฅผ joinํ•จ
select user.user_id, user.name, addr.addr from user left join addr on user.user_id = addr.user_id
SQL
๋ณต์‚ฌ
3) Right Join
โ€ข
์ฃผ์†Œ๋งŒ ์žˆ๊ณ  user_id, user.name์ด null๊ฐ’์ธ row ์ƒ์„ฑ
select user.user_id, user.name, addr.addr from user right join addr on user.user_id = addr.user_id
SQL
๋ณต์‚ฌ

5. Union

-- union select name from user union select addr from addr -- union all: ์ค‘๋ณต์ด ์ œ๊ฑฐ๋˜์ง€ ์•Š์Œ select name from user union all select addr from addr -- union์„ ์ด์šฉํ•œ full outer join -- (syntax: left join ๊ฒฐ๊ณผ union right join ๊ฒฐ๊ณผ) select user.name, addr.addr from user left join addr on user.user_id = addr.user_id union select user.name, addr.addr from user right join addr on user.user_id = addr.user_id
SQL
๋ณต์‚ฌ

6. Sub-query

โ€ข
sub-query๋Š” query๋ฌธ ์•ˆ์— ์‚ฌ์šฉํ•˜๋Š” query
โ€ข
select์ ˆ, from์ ˆ, where์ ˆ ๋“ฑ์—์„œ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅ
1) select์ ˆ์— ์‚ฌ์šฉ
use world -- SELECT์ ˆ์— ์‚ฌ์šฉ: ์ „์ฒด ๋‚˜๋ผ์ˆ˜, ๋„์‹œ์ˆ˜, ์–ธ์–ด์ˆ˜๋ฅผ ํ•˜๋‚˜์˜ row๋กœ ์ถœ๋ ฅํ•˜๊ธฐ select (select count(*) from country) as country_count, (select count(*) from city) as city_count, (select count(distinct(language)) from countrylanguage) as language_count from dual -- from dual์€ ์•ˆ ์จ์ค˜๋„ ok
SQL
๋ณต์‚ฌ
2) from์ ˆ์— ์‚ฌ์šฉ
-- 800๋งŒ ์ด์ƒ์ด ๋˜๋Š” ๋„์‹œ์˜ ๊ตญ๊ฐ€์ฝ”๋“œ ์ด๋ฆ„, ๋„์‹œ์ธ๊ตฌ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ -- ์‚ฌ์šฉํ•  sub-query select countrycode, name, population from city where population > 8000000 -- filtering์„ ๋จผ์ € ํ•œ ํ›„์— join -- joinํ•  ๋•Œ๋Š” ์ตœ๋Œ€ํ•œ ์ ์€ ๋ฐ์ดํ„ฐ๋ผ๋ฆฌ ํ•˜๋Š” ๊ฒƒ์ด ์ฟผ๋ฆฌ ์„ฑ๋Šฅ ํ–ฅ์ƒ์— ๋„์›€ select city.countrycode, city.name, country.name, city.population from (select countrycode, name, population from city where population > 8000000) as city join country on city.countrycode = coutry.code -- join์„ ํ•œ ํ›„์— filtering select city.countrycode, city.name, country.name, city.population from city join country on city.countrycode = coutry.code having city.population > 8000000
SQL
๋ณต์‚ฌ
3) where์ ˆ์— ์‚ฌ์šฉ
-- ์ธ๊ตฌ 800๋งŒ ์ด์ƒ ๋„์‹œ์˜ ๊ตญ๊ฐ€์ฝ”๋“œ, ๊ตญ๊ฐ€์ด๋ฆ„, ๋Œ€ํ†ต๋ น ์ด๋ฆ„์„ ์ถœ๋ ฅ select code, name, headofstate from country where code in ( select countrycode from city where population > 8000000 )
SQL
๋ณต์‚ฌ

7. Index

โ€ข
ํ…Œ์ด๋ธ”์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒ€์ƒ‰ํ•  ๋•Œ ๋น ๋ฅด๊ฒŒ ์ฐพ์„ ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ
โ€ข
where์ ˆ์— ๋“ค์–ด๊ฐ€๋Š” ์ปฌ๋Ÿผ์„ index๋กœ ์„ค์ •ํ•ด ๋†“์œผ๋ฉด ์„ค์ •ํ•œ ์ปฌ๋Ÿผ์„ ์กฐ๊ฑด์œผ๋กœ ๊ฒ€์ƒ‰ํ•  ๋•Œ ๋น ๋ฅด๊ฒŒ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ์Œ
โ€ข
๋„ˆ๋ฌด ๋งŽ์€ index๊ฐ€ ์„ค์ •๋˜๋ฉด ๋ฐ์ดํ„ฐ๊ฐ€ ์ž…๋ ฅ๋  ๋•Œ๋งˆ๋‹ค index์— ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด์•ผํ•˜๋ฏ€๋กœ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ์‹œ ์†๋„๊ฐ€ ๋Š๋ ค์งˆ ์ˆ˜ ์žˆ์Œ
โ€ข
๊ทธ๋Ÿฌ๋ฏ€๋กœ ๊ฒ€์ƒ‰์กฐ๊ฑด์œผ๋กœ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ์ปฌ๋Ÿผ์— ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Œ
-- syntax CREATE INDEX ์ธ๋ฑ์Šค์ด๋ฆ„ ON ํ…Œ์ด๋ธ”์ด๋ฆ„ (์ปฌ๋Ÿผ์ด๋ฆ„1, ์ปฌ๋Ÿผ์ด๋ฆ„2) -- example -- city ํ…Œ์ด๋ธ”์— population ์ปฌ๋Ÿผ์„ index๋กœ ์ถ”๊ฐ€ create index Population on city (population) -- explain: query๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์ „์— index๋กœ ๊ฒ€์ƒ‰์„ ํ•˜๋Š”์ง€ ํ™•์ธ ๊ฐ€๋Šฅ # 100๋งŒ์ด ๋„˜๋Š” ๋„์‹œ์˜ ๋ฐ์ดํ„ฐ์˜ ์ถœ๋ ฅ ์‹คํ–‰๊ณ„ํš์„ ํ™•์ธ explain select * from city where population >= 1000000 -- example: employees db๋กœ ํ…Œ์ŠคํŠธ use employees explain select * from salaries where salary > 60000 and to_date > "2000-01-01" order by salary limit 1000 create index salaries on salaries (salary)
SQL
๋ณต์‚ฌ

8. View

โ€ข
๊ฐ€์ƒ์˜ ํ…Œ์ด๋ธ”๋กœ ํŠน์ • ๋ฐ์ดํ„ฐ๋งŒ ๋ณด๊ณ ์žํ•  ๋•Œ ์‚ฌ์šฉ (ํŠน์ • ์ปฌ๋Ÿผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ์—ญํ• ๋งŒ ์ˆ˜ํ–‰)
โ€ข
view๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ query๋ฅผ ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Œ
โ€ข
์ปฌ๋Ÿผ์˜ ์ˆ˜์ •์ด๋‚˜ ์ธ๋ฑ์Šค ์„ค์ • ๊ฐ™์€ ๋ช…๋ น์„ ํ•  ์ˆ˜ ์—†์Œ
-- syntax CREATE VIEW <view ์ด๋ฆ„> AS <QUERY> -- example use world select * from countrylanguage join( select country.code, country.name as country_name, city.name as city_name from country join city on country.code = city.countrycode ) as s1 on s1.code = countrylanguage.countrycode create view code_name as select country.code, country.name as country_name, city.name as city_name from country join city on country.code = city.countrycode select * from countrylanguage join code_name on code_name.code = countrylanguage.countrycode
SQL
๋ณต์‚ฌ
์ฐธ๊ณ  ์ž๋ฃŒ
โ€ข
ํŒจ์ŠคํŠธ์บ ํผ์Šค '๋ฐ์ดํ„ฐ ์‚ฌ์ด์–ธ์Šค ์Šค์ฟจ Python 8๊ธฐ' ์ˆ˜์—…์ž๋ฃŒ