๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Data Base/MySQL

MySQL 15๋ฒˆ์งธ์ˆ˜์—…

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 8.
๋ฐ˜์‘ํ˜•

โ–ถ SELECT ๋ช…๋ น๋ฌธ ์ผ๋ถ€ ์ ˆ์„ ํฌํ•จํ•œ ์ˆ˜ํ–‰ ๊ณผ์ •

โ€‹

์˜ˆ์ œ 7-3) ์ˆ˜๊ฐ•์‹ ์ฒญ ํ…Œ์ด๋ธ”(attend)์—์„œ 2006๋…„๋„ 1ํ•™๊ธฐ์— ์ˆ˜๊ฐ• ์‹ ์ฒญํ•œ ํ•™์ƒ์˜ ํ•™๋ฒˆ๊ณผ ์ˆ˜๊ฐ•๋…„๋„, ํ•™๊ธฐ, ๊ต๊ณผ๋ชฉ์ฝ”๋“œ, ๊ต์ˆ˜์ฝ”๋“œ๋ฅผ ๊ต์ˆ˜์ฝ”๋“œ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๋‚˜ํƒ€๋‚ด์–ด๋ผ.

โ€‹

mysql> select stu_no, att_year, att_term, sub_code, prof_code

→ from attend

→ where att_year = '2016' and att_term = 1

→ order by prof_code;

 

โ€‹

โ€‹

<์—ฐ์Šต๋ฌธ์ œ>

 

7-1) SELECT ๋ช…๋ น๋ฌธ์—๋Š” 6๊ฐœ์˜ ์ ˆ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค. 6๊ฐœ ์ ˆ ์ค‘์—์„œ ์ƒ๋žต์ด ๊ฐ€๋Šฅํ•œ ์ ˆ์€ ๋ฌด์—‡์ธ๊ฐ€ ?

A) group by, having, where, order by ์ ˆ. select์ ˆ๊ณผ from์ ˆ ์ด์™ธ๋Š” ์ƒ๋žต ๊ฐ€๋Šฅ

โ€‹

โ€‹

7-2. HAVING์ ˆ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฐ˜๋“œ์‹œ ์–ด๋–ค ์ ˆ์ด ๋จผ์ € ๊ธฐ์ˆ ๋˜์–ด์•ผ ํ•˜๋Š”๊ฐ€ ?

A) group by์ ˆ

โ€‹

โ€‹

7-3) SELECT ๋ช…๋ น๋ฌธ์—๋Š” 6๊ฐœ์˜ ์ ˆ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค. 6๊ฐœ์˜ ์ ˆ์„ ์ „์ฒด ๊ธฐ์ˆ ๋œ SELECT ๋ช…๋ น๋ฌธ์ด ์žˆ๋Š” ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ ๊ณผ์ •์„ ์ˆœ์„œ์ ์œผ๋กœ ๋‚˜ํƒ€๋‚ด์–ด๋ผ.

A) from → where → group by → having → select → order by

โ€‹

โ€‹

7-4) ๋‹ค์Œ SELECT ๋ช…๋ น๋ฌธ์˜ ์ ˆ์˜ ์ฒ˜๋ฆฌ๊ณผ์ •์„ ์ˆœ์„œ์ ์œผ๋กœ ํ‘œํ˜„ํ•˜๋ผ.

โ€‹

โ€‹

โ€‹โ€‹

โ–ถ SELECT ๋ช…๋ น๋ฌธ : FROM ์ ˆ

โ€‹

1. FROM ์ ˆ์—์„œ ํ…Œ์ด๋ธ” ๋ช…์„ธ (๋‚˜์ค‘์— ๋˜๋Œ์•„์™€์„œ ๋ฐฐ์›€)

โ€‹

 

2. ์—ด ๋ช…์„ธ

SELECT ๋ช…๋ น๋ฌธ์—์„œ ์—ด์˜ ์ด๋ฆ„ ์•ž์— ์—ด์ด ํฌํ•จ๋˜์–ด ์žˆ๋Š” ํ…Œ์ด๋ธ”์˜ ์ด๋ฆ„์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ฒƒ์„ ์—ด์˜ ์ž๊ฒฉ์ด๋ผ ํ•œ๋‹ค.

์˜ˆ) student.stu_no

โ€‹โ€‹

 

3. ๋‹ค์ค‘ ํ…Œ์ด๋ธ” ๋ช…์„ธ

์ง€๊ธˆ๊นŒ์ง€๋Š” FROM ์ ˆ์— ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ” ๋ช…์„ธ๋งŒ ์‚ฌ์šฉํ•˜์˜€๋‹ค. ๋งŒ์•ฝ ๊ฒฐ๊ณผ ํ…Œ์ด๋ธ”์— ์„œ๋กœ ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋ ค ํ•œ๋‹ค๋ฉด FROM ์ ˆ์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ…Œ์ด๋ธ”์„ ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค.

โ€‹

โ€‹โ€‹

์˜ˆ์ œ 8-3) ๊ฐ ํ•™์ƒ์˜ ํ•™๋ฒˆ๊ณผ ์ด๋ฆ„, ์ˆ˜๊ฐ•๋…„๋„, ํ•™๊ธฐ, ์ˆ˜๊ฐ•๊ณผ๋ชฉ์ฝ”๋“œ, ๊ต์ˆ˜์ฝ”๋“œ๋ฅผ ๋‚˜ํƒ€๋‚ด์–ด๋ผ.

A)

mysql> select student.stu_no, stu_name,

→ att_year, att_term, sub_code, prof_code

→ from student, attend

→ where student.stu_no = attend.stu_no;

โ€‹โ€‹

โ€‹

์˜ˆ์ œ 8-4) ํ•™์ƒ๋“ค์˜ ํ•™๋ฒˆ, ์ด๋ฆ„, ์ˆ˜๊ฐ•์‹ ์ฒญ๊ตฌ๋ถ„์„ ๋‚˜ํƒ€๋‚ด์–ด๋ผ. ๋‹จ, ์ˆ˜๊ฐ•์‹ ์ฒญ๊ตฌ๋ถ„์€ attend ํ…Œ์ด๋ธ”์— ์žˆ๋‹ค.

A)

mysql> select s.stuโ€‹_no, stu_name, att_div

→ from student s, attend aโ€‹

→ where s.stu_no = a.stโ€‹u_no;

โ€‹โ€‹

โ€‹

ํ€ด์ฆˆ) ์ถœ๋ ฅ๋ฌธ์ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜์˜ค๊ฒŒ ๋ช…๋ น๋ฌธ์„ ์ ์–ด๋ณด์‹œ์˜ค.

ํžŒํŠธ. ํ…Œ์ด๋ธ” 4๊ฐœ ์‚ฌ์šฉํ•ด์•ผํ•จ

A)

mysql> select s1.stu_no, stu_name, att_year, att_term, s2.sub_code, sub_name, p.prโ€‹of_code, prof_name

→ from student s1, attend a, subject s2, professor p

→ where s1.stu_no = a.stu_no โ€‹and a.sub_code = s2.sub_code and a.prof_code = p.prof_code;

โ€‹

โ˜…์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ”์„ ๋ถˆ๋Ÿฌ์˜ฌ๋•Œ๋Š” ์• ๋งค๋ชจํ˜ธํ•จ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ํ…Œ์ด๋ธ” ์•ž์— ํ…Œ์ด๋ธ” ์ด๋ฆ„์„ ์ง€์ •ํ•ด์ฃผ๋Š”๊ฒŒ ์ข‹๋‹ค.

โ€‹โ€‹

โ€‹

โ€‹

4. ๊ฐ€๋ช…(Alias)

โ€‹

์˜ˆ์ œ 8-5) ํ•™์ƒ๋“ค์˜ ํ•™๋ฒˆ, ์ด๋ฆ„, ๋“ฑ๋ก์—ฌ๋ถ€๋ฅผ ๋‚˜ํƒ€๋‚ด์–ด๋ผ. ๋‹จ, ํ•™์  ํ…Œ์ด๋ธ”์˜ ๊ฐ€๋ช…์„ s, ๋“ฑ๋กํ…Œ์ด๋ธ”์˜ ๊ฐ€๋ช…์€ f๋กœ ์ •์˜ํ•œ๋‹ค.

A)

mysql> select s.stu_no, stuโ€‹_name, fee_div

→ from student s, fee fโ€‹

→ whereโ€‹ s.stu_no = f.stu_no;

โ€‹โ€‹โ€‹

 

โ€‹

5. FROM ์ ˆ์˜ ๋‹ค์–‘ํ•œ ์˜ˆ์ œโ€‹

โ€‹

์˜ˆ์ œ 8-6) ์ˆ˜๊ฐ•ํ…Œ์ด๋ธ”์˜ ํ•™๋ฒˆ๊ณผ ์ด๋ฆ„, ์ˆ˜๊ฐ•๊ณผ๋ชฉ์ฝ”๋“œ, ๊ต์ˆ˜์ฝ”๋“œ, ๊ต์ˆ˜๋ช…์„ ๋‚˜ํƒ€๋‚ด์–ด๋ผ.

A)

mysql> select s.stu_no, stu_name, sub_code, p.prโ€‹of_code, prof_name

→ from student s, attend a, professor pโ€‹

→ where s.stu_no = a.stu_no and

→ a.prof_code = p.proโ€‹f_code;

โ€‹

โ€‹

์˜ˆ์ œ 8-7) ํ•™์ ํ…Œ์ด๋ธ”์˜ ํ•™๋ฒˆ๊ณผ ์ด๋ฆ„, ์ˆ˜๊ฐ•ํ…Œ์ด๋ธ”์˜ ์ˆ˜๊ฐ•๊ต๊ณผ๋ชฉ์ฝ”๋“œ, ๊ต๊ณผ๋ชฉํ…Œ์ด๋ธ”์˜ ๊ต๊ณผ๋ชฉ๋ช…์„ ๋‚˜ํƒ€๋‚ด์–ด๋ผ.

A)

mysql> select s.stu_no, stu_name, att_year, att_term, a.sub_coโ€‹de, sub_name

→ from student s, attend a, subject suโ€‹

→ where s.stu_no = a.stu_no and

→ a.sub_code = su.sub_cโ€‹ode;

โ€‹โ€‹

โ€‹

์˜ˆ์ œ 8-8) ํ•™์ ํ…Œ์ด๋ธ”์˜ ํ•™๋ฒˆ๊ณผ ์ด๋ฆ„, ๋ณด๊ด€์„ฑ์ ํ…Œ์ด๋ธ”์˜ ์„ฑ์  ์ทจ๋“๋…„๋„, ํ•™๊ธฐ, ์‹ ์ฒญํ•™์ , ์ทจ๋“ํ•™์ , ํ‰์ ํ‰๊ท ์„ ๋‚˜ํƒ€๋‚ด์–ด๋ผ.

A)

mysql> select s.stu_no, stu_name, sco_year, sco_term,

→ req_point, take_point, exam_avg

→ from student s, score scโ€‹

→ where s.stu_no = sc.stโ€‹u_no;

โ€‹

โ€‹โ€‹

์˜ˆ์ œ 8-9) ํ•™์  ํ…Œ์ด๋ธ”์˜ ํ•™๋ฒˆ๊ณผ ์ด๋ฆ„, ์ˆ˜๊ฐ•ํ…Œ์ด๋ธ”์˜ ์ˆ˜๊ฐ•์‹ ์ฒญ๋…„๋„, ํ•™๊ธฐ, ์ˆ˜๊ฐ•์‹ ์ฒญ์œ ๋ฌด๋ฅผ ๋‚˜ํƒ€๋‚ด์–ด๋ผ.

A)

mysql> select s.โ€‹stu_no, stu_name, att_year, att_term, att_div

→ from student s, attend aโ€‹

→ where s.stu_no = a.โ€‹stu_no;

-์ค‘๋ณต๋œ ์—ด์„ ์ œ๊ฑฐํ•˜๊ธฐ์œ„ํ•ด DISTINCT ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์ž.

โ€‹

โ€‹

์˜ˆ์ œ 8-10) ํ•™์ ํ…Œ์ด๋ธ”์˜ ํ•™๋ฒˆ๊ณผ ์ด๋ฆ„, ์ˆ˜๊ฐ•ํ…Œ์ด๋ธ”์˜ ์ˆ˜๊ฐ•์‹ ์ฒญ๋…„๋„, ํ•™๊ธฐ, ์ˆ˜๊ฐ•์‹ ์ฒญ์œ ๋ฌด๋ฅผ ๋‚˜ํƒ€๋‚ด์–ด๋ผ.

(๋‹จ, ์ค‘๋ณต์€ ๋ฐฐ์ œํ•œ๋‹ค.)

A)

mysql> select distinctโ€‹ s.stu_no, stu_name, att_year, att_term, att_div

→ from student s, attend aโ€‹

→ where s.stu_no = a.sโ€‹tu_no;

โ€‹

โ€‹

์˜ˆ์ œ 8-11) ์ ์–ด๋„ ํ•œ ๋ฒˆ ์ด์ƒ ์žฅํ•™๊ธˆ์„ ๋ฐ›์€ ํ•™์ƒ์˜ ์ด๋ฆ„์„ ๋‚˜ํƒ€๋‚ด์–ด๋ผ.

A)

mysql> select distinctโ€‹ s.stu_no, stu_name

→ from student s, fee fโ€‹

→ where s.stu_no = f.stu_no and

→ not jang_code is null;

โ˜…is null : ๊ฐ’์ด null์ธ ๊ฒฝ์šฐ

in not null : ๊ฐ’์ด null์ด ์•„๋‹ ๊ฒฝ์šฐ

โ€‹โ€‹

โ€‹

์˜ˆ์ œ 8-12) ๋“ฑ๋กํ•œ ํ•™์ƒ์˜ ์ด๋ฆ„, ๋“ฑ๋ก๋…„๋„, ํ•™๊ธฐ, ์žฅํ•™์ฝ”๋“œ, ์žฅํ•™๊ธˆ์ด์•ก, ๋“ฑ๋ก๊ตฌ๋ถ„์„ ๋‚˜ํƒ€๋‚ด์–ด๋ผ.

A)

mysql> select stu_name, fee_year, fee_term, jang_code, jang_total, fee_div

→ from student s, fee fโ€‹

→ where s.stu_no = f.stโ€‹u_no

andโ€‹ f.fโ€‹ee_div = 'Y';

โ€‹

โ€‹

์˜ˆ์ œ 8-13) 2019๋…„์— ๋“ฑ๋กํ•œ ํ•™์ƒ์˜ ํ•™๋ฒˆ๊ณผ ์ด๋ฆ„์„ ๋‚˜ํƒ€๋‚ด์–ด๋ผ.

A)

mysql> select distinct s.stu_no, s.stu_name

→ from student s,fee fโ€‹

→ where s.stu_no = f.stuโ€‹_no

and fee_year = 2019;

โ€‹โ€‹

โ€‹

โ€‹

โ–ถ ๋ฐ˜๋“œ์‹œ ๊ฐ€๋ช…์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ

โ€‹

์˜ˆ์ œ 8-14) ์žฅ์ˆ˜์ธ(1999๋…„ 02์›” 09์ผ) ํ•™์ƒ๋ณด๋‹ค ๋จผ์ € ํƒœ์–ด๋‚œ ํ•™์ƒ์˜ ์ด๋ฆ„๊ณผ ์ƒ๋…„์›”์ผ์„ ๋‚˜ํƒ€๋‚ด์–ด๋ผ.

A)

mysql> select s.stu_name, s.birthday

→ from student s, student stโ€‹

→ where st.sโ€‹tu_name = '์žฅ์ˆ˜์ธ'

andโ€‹ s.birthday < st.biโ€‹rthday;

 

๋ฐ˜์‘ํ˜•

'Data Base > MySQL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

MySQL 17๋ฒˆ์งธ์ˆ˜์—…  (0) 2021.02.08
MySQL 16๋ฒˆ์งธ์ˆ˜์—…  (0) 2021.02.08
MySQL 14๋ฒˆ์งธ์ˆ˜์—…  (0) 2021.02.08
MySQL 13๋ฒˆ์งธ์ˆ˜์—…  (0) 2021.02.08
MySQL 12๋ฒˆ์งธ์ˆ˜์—…  (0) 2021.02.08

๋Œ“๊ธ€