λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
Data Base/MySQL

MySQL 23λ²ˆμ§Έμˆ˜μ—…

by μ½”λ”©ν•˜λŠ” λΆ•μ–΄ 2021. 2. 9.
λ°˜μ‘ν˜•

β–Ά SELECT λͺ…λ Ήλ¬Έ : 쑰인(JOIN)

​

*쑰인(JOIN)

ν…Œμ΄λΈ”μ˜ μ‘°μΈμ—μ„œ ν…Œμ΄λΈ”μ— μžˆλŠ” 행은 μ‘°ν•©λ˜μ–΄ κ²°κ³Ό ν…Œμ΄λΈ”μ„ μƒμ„±ν•œλ‹€.

SELECT λͺ…λ Ήλ¬Έμ˜ FROM μ ˆμ— 적어도 두 개의 ν…Œμ΄λΈ” λͺ…μ„Έκ°€ 있고, WHERE μ ˆμ—λŠ” μ„œλ‘œ λ‹€λ₯Έ ν…Œμ΄λΈ”μ˜ 열을 λΉ„κ΅ν•˜λŠ” 쑰건이 적어도 ν•˜λ‚˜κ°€ μžˆλ‹€λ©΄ 이λ₯Ό 쑰인(JOIN)이라 ν•œλ‹€.

​

​

예제 15-1) 학적 ν…Œμ΄λΈ”μ— μ‘΄μž¬ν•˜λŠ” 학생듀 μ€‘μ—μ„œ 아직 등둝을 ν•˜μ§€ λͺ»ν•œ 학생이 μžˆλ‹€. 각 학생에 λŒ€ν•˜μ—¬ ν•™λ²ˆ, 이름, 등둝년도, ν•™κΈ°, 학생이 λ“±λ‘ν•œ λ‚©μž…κΈˆ 총앑을 좜λ ₯ν•˜λΌ.​

A)

mysql> select student.stu_no, stu_name, fee_year, fee_term, fee_pay

-> from student inner join fee

-> on student.stu_no = fee.stu_no;

​

​

예제 15-2) 학적 ν…Œμ΄λΈ”κ³Ό 성적 ν…Œμ΄λΈ”μ„ cross joinν•˜μ—¬ ν•™λ²ˆ, 이름, 성적년도, ν•™κΈ°λ₯Ό 좜λ ₯ν•˜λΌ.

A)

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

-> from student s inner join score sc;

​

​

예제 15-3) 학적 ν…Œμ΄λΈ”κ³Ό 성적 ν…Œμ΄λΈ”μ„ cross joinν•˜μ—¬ ν•™λ²ˆ, 이름, 성적년도, ν•™κΈ°λ₯Ό 좜λ ₯ν•˜λΌ.(단, 학적 ν…Œμ΄λΈ”μ— μ‘΄μž¬ν•˜λŠ” '20161001' ν•™μƒλ§Œμ„ 좜λ ₯ν•˜λΌ.)

A)

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

-> from student s inner join score sc

-> on s.stu_no = '20161001';

​

​

예제 15-4) 학적 ν…Œμ΄λΈ”μ— μ‘΄μž¬ν•˜λŠ” 학생듀 μ€‘μ—μ„œ λ“±λ‘ν•œ ν•™μƒμ˜ ν•™λ²ˆ, 이름, 반, 등둝년도, ν•™κΈ°, λ“±λ‘κΈˆ 총앑을 좜λ ₯ν•˜λΌ.

A)

inner join으둜 좜λ ₯

mysql> select s.stu_no, stu_name, class, fee_year, fee_term, fee_pay

-> from student s inner join fee f

-> on s.stu_no = f.stu_no

-> order by s.stu_no, fee_year, fee_term;

​

A)

left outer join으둜 좜λ ₯

mysql> select s.stu_no, stu_name, class, fee_year, fee_term, fee_pay

-> from student s left outer join fee f

-> on s.stu_no = f.stu_no

-> order by s.stu_no, fee_year, fee_term;

​

​

예제 15-5) μˆ˜κ°• ν…Œμ΄λΈ”μ— μ‘΄μž¬ν•˜λŠ” ν•™μƒμ˜ μˆ˜κ°•μ‹ μ²­λ…„λ„, ν•™κΈ°, κ³Όλͺ©μ½”λ“œ, κ³Όλͺ©λͺ…을 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, att_year, att_term, a.sub_code, sub_name

-> from attend a inner join subject s

-> on a.sub_code = s.sub_code;

​

​

예제 15-6) μž¬ν•™μƒμ΄λ©΄μ„œ μˆ˜κ°•μ‹ μ²­ν…Œμ΄λΈ”μ— μ‘΄μž¬ν•˜λŠ” ν•™μƒμ˜ ν•™λ²ˆ, 이름, μˆ˜κ°•μ‹ μ²­λ…„λ„, ν•™κΈ°, κ³Όλͺ©μ½”λ“œ, κ³Όλͺ©λͺ…, κ΅μˆ˜μ½”λ“œ, ꡐ수λͺ…을 좜λ ₯ν•˜λΌ. (단, 좜λ ₯μˆœμ„œλŠ” ν•™λ²ˆ, μˆ˜κ°•λ…„λ„, μˆ˜κ°•ν•™κΈ°, μˆ˜κ°•μ½”λ“œ 순이고, κ³Όλͺ©λͺ…κ³Ό ꡐ수λͺ…을 ν‘œμ œμ–΄λ‘œ μ‚¬μš©ν•˜λΌ.)

A)

mysql> select s.stu_no, stu_name, att_year, att_term, a.sub_code, sub_name κ³Όλͺ©λͺ…,

-> p.prof_code, prof_name ꡐ수λͺ…

-> from student s inner join attend a inner join subject b inner join professor p

-> on s.stu_no = a.stu_no and a.sub_code = b.sub_code and a.prof_code = p.prof_code

-> order by s.stu_no, att_year, att_term, a.sub_code;

​

​

예제 15-7) 학적 ν…Œμ΄λΈ”μ— μ‘΄μž¬ν•˜λŠ” λͺ¨λ“  학생이 좜λ ₯될 수 μžˆλ„λ‘ left join으둜 좜λ ₯ν•˜λΌ. (단, λ―Έλ“±λ‘μžλŠ” 등둝년도λ₯Ό "λ―Έλ‚©", ν•™κΈ°λŠ” 0, λ‚©μž…κΈˆ 총앑도 0으둜 좜λ ₯ν•˜λΌ.)

A)

mysql> select student.stu_no, stu_name, fee_year, fee_term, fee_pay

-> from student left join fee

-> on student.stu_no = fee.stu_no

-> union

-> select stu_no, stu_name, 'λ―Έλ‚©', 0, 0

-> from student

-> where stu_no not in

-> (select stu_no from fee);

​

​

​

<μ—°μŠ΅λ¬Έμ œ>

​

15-2) 각 학생에 λŒ€ν•˜μ—¬ ν•™λ²ˆ, 이름, λ“±λ‘κΈˆ μ΄μ•‘μ˜ 합을 κ΅¬ν•˜μ—¬λΌ.(단, λ“±λ‘κΈˆμ„ λ‚©λΆ€ν•˜μ§€ λͺ»ν•œ 학생은 0으둜 좜λ ₯ν•˜λΌ)

A)

mysql> select s.stu_no, stu_name, ifnull(sum(fee_pay), 0)

-> from student s left join fee f

-> on s.stu_no = f.stu_no

-> group by s.stu_no;

​

​

15-3) 각 학생에 λŒ€ν•˜μ—¬ ν•™λ²ˆ, 이름, 동아리 λͺ…을 좜λ ₯ν•˜λΌ.(단, μž¬ν•™μƒμ΄λ©΄μ„œ 동아리에 κ°€μž…ν•˜μ§€ μ•Šμ€ 학생듀은 "λ―Έκ°€μž…"을 좜λ ₯ν•˜λΌ. left join으둜 좜λ ₯​)

A)

mysql> select s.stu_no, s.stu_name, ifnull(cir_name, 'λ―Έκ°€μž…')

-> from student s left join circle c

-> on s.stu_no = c.stu_no;

​​

​

​​

β–Ά ν…Œμ΄λΈ”μ˜ λ³€κ²½

​

1. μƒˆλ‘œμš΄ ν–‰ μ‚½μž…

​​

예제 16-1) κ΅μˆ˜μ½”λ“œ "6001"κ³Ό κ΅μˆ˜μ΄λ¦„ "λŒ€μ‘°μ˜"을 μΆ”κ°€ν•˜λΌ. (단, λ°μ΄ν„°λŠ” κ΅μˆ˜μ½”λ“œμ™€ ꡐ수λͺ…λ§Œ μž…λ ₯ν•œλ‹€.)

A)

mysql> insert into professor(prof_code, prof_name)

-> values('6001', 'λŒ€μ‘°μ˜');

Query OK, 1 row affected (0.01 sec)

​

​

예제 16-2) μƒˆλ‘œμš΄ κ³Όλͺ©(JSP Programming)을 μΆ”κ°€ν•˜λΌ. 단, λ°μ΄ν„°λŠ” λ‹€μŒκ³Ό κ°™λ‹€.

A)

mysql> insert into subject values

-> ('5001', 'JSP ν”„λ‘œκ·Έλž˜λ°', 'JSP Programming', 2006);

Query OK, 1 row affected (0.00 sec)

​​

 

​

2. ν…Œμ΄λΈ”μ—μ„œ ν…Œμ΄λΈ”λ‘œ ν–‰ 볡사

​

예제 16-3) μƒˆλ‘œμš΄ ν˜Έλ‚¨(κ΄‘μ£Ό, 전라남도, 전라뢁도, μ œμ£Όλ„) ν–₯우회 ν…Œμ΄λΈ”μ„ μƒμ„±μ‹œμΌœλ³΄μž. ν–₯우회 ν…Œμ΄λΈ”μ€ ν•™μƒμ˜ ν•™λ²ˆ, 이름, 우편번호, ν˜„μ£Όμ†Œ, νœ΄λŒ€ν°λ²ˆν˜Έλ‘œ κ΅¬μ„±λ˜μ–΄μžˆλ‹€.

A)

​

(1) λ¨Όμ € μƒˆλ‘œμš΄ ν…Œμ΄λΈ”μ„ μƒμ„±ν•œλ‹€

​

mysql> create table hang(

-> stu_no char(10) Not null,

-> stu_name char(10) Not null,

-> hyun_post_no varchar(6) Not null,

-> hyun_add varchar(100) Not null,

-> hyun_mobile varchar(14),

-> primary key (stu_no)

-> ) engine = innoDB;

Query OK, 0 rows affected (0.02 sec)

​

​

(2) λ‹€μŒ INSERT λͺ…령문은 STUDENT ν…Œμ΄λΈ”μ— λ“±λ‘λ˜μ–΄μžˆλŠ” ν˜Έλ‚¨μ§€μ—­(ν˜„μ£Όμ†Œ 우편번호 첫 번째 1μžλ¦¬κ°€ κ΄‘μ£Ό, μ œμ£ΌλŠ” 6이고 전남, 전뢁은 5둜 μ‹œμž‘ν•˜λŠ”)의 학생에 κ΄€ν•œ 정보λ₯Ό HANG ν…Œμ΄λΈ”λ‘œ λ³΅μ‚¬ν•œλ‹€.

​

mysql> insert into hang

-> (stu_no, stu_name, hyun_post_no, hyun_add, hyun_mobile)

-> select stu_no, stu_name, post_no, address, mobile

-> from student

-> where substring(post_no, 1, 1) = 5 or substring(post_no, 1, 1) = 6;

Query OK, 4 rows affected (0.01 sec)

Records: 4 Duplicates: 0 Warnings: 0

​

​

(3) λ‹€μŒμ€ ν˜Έλ‚¨μ§€μ—­μ— μ‚¬λŠ” ν•™μƒλ“€μ˜ λ‚΄μš©μ„ ν™•μΈν•˜μ—¬ 보자.

​

​​

​

3. ν–‰ κ°’μ˜ λ³€κ²½

​

예제 16-5) μžλ°”ν”„λ‘œκ·Έλž˜λ°μ˜ 생성년도λ₯Ό 2010λ…„μœΌλ‘œ κ°±μ‹ ν•˜λΌ.

A)

mysql> update subject

-> set create_year = '2010'

-> where sub_name = 'μžλ°”ν”„λ‘œκ·Έλž˜λ°';

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

​

​

예제 16-6) λͺ¨λ“  λ“±λ‘κΈˆ 총앑을 5% μ¦κ°€μ‹œμΌœλΌ.

A)

mysql> update fee

-> set fee_total = fee_total * 1.05;

Query OK, 24 rows affected (0.01 sec)

Rows matched: 24 Changed: 24 Warnings: 0

​

​

예제 16-7) μ΄μˆœμ‹  학생이 μ„œμšΈνŠΉλ³„μ‹œ 노원ꡬ μ„κ³„λ‘œ ν˜„λŒ€μ•„νŒŒνŠΈ 2동 1004ν˜Έμ—μ„œ μ„œμšΈνŠΉλ³„μ‹œ μ˜λ“±ν¬κ΅¬ μ˜λ“±ν¬λ‘œ79κ°€κΈΈ 10 λͺ¨λ˜ν•˜μš°μŠ€ 403호둜 μ΄μ‚¬ν•˜μ˜€λ‹€. μš°νŽΈλ²ˆν˜ΈλŠ” 07318이닀. 데이터λ₯Ό λ³€κ²½ν•˜λΌ.

A)

mysql> update student

-> set address = 'μ„œμšΈνŠΉλ³„μ‹œ μ˜λ“±ν¬κ΅¬ μ˜λ“±ν¬λ‘œ79κ°€κΈΈ 10 λͺ¨λ˜ν•˜μš°μŠ€ 403호',

-> post_no = '07318'

-> where stu_name = 'μ΄μˆœμ‹ ';

Query OK, 1 row affected (0.01 sec)

Rows matched: 1 Changed: 1 Warnings: 0

​

​

예제 16-8) 동아리에 κ°€μž…ν•œ 학생듀을 λŒ€μƒμœΌλ‘œ 동아리 νšŒλΉ„λ₯Ό 5000원 λ“±λ‘κΈˆμ— μΆ”κ°€ν•˜μ—¬ λ‚©λΆ€ν•˜λ„λ‘ ν•˜κ³ , 등둝 ν…Œμ΄λΈ”μ˜ FEE_TOTALκ³Ό FEE_PAY도 λ³€κ²½ν•˜λΌ.

A)

mysql> update fee

-> set fee_price = fee_price + 5000,

-> fee_total = fee_total + 5000,

-> fee_pay = fee_pay + 5000

-> where stu_no in

-> (select stu_no from circle); → 동아리에 κ°€μž…ν•œ ν•™μƒλ“€μ—κ²Œλ§Œ μ μš©λ˜λ„λ‘

Query OK, 14 rows affected (0.01 sec)

Rows matched: 14 Changed: 14 Warnings: 0

​

​

μ μš©λ˜μ—ˆλ‚˜ 확인해보기↓

A)

mysql> select stu_no, fee_year, fee_term, fee_price, fee_total, fee_pay

-> from fee

-> where stu_no in

-> (select stu_no from circle);

​

​

예제 16-9) "운영체제" κ³Όλͺ©μ„ μ‚­μ œν•˜λΌ.

A)

 

λ°˜μ‘ν˜•

'Data Base > MySQL' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

MySQL 25λ²ˆμ§Έμˆ˜μ—…  (0) 2021.02.09
MySQL 24λ²ˆμ§Έμˆ˜μ—…  (0) 2021.02.09
MySQL 22λ²ˆμ§Έμˆ˜μ—…  (0) 2021.02.09
MySQL 21λ²ˆμ§Έμˆ˜μ—…  (0) 2021.02.09
MySQL 20λ²ˆμ§Έμˆ˜μ—…  (0) 2021.02.09

λŒ“κΈ€