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

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

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

β–Ά 뢀속 μ§ˆμ˜μ–΄

뢀속 μ§ˆμ˜μ–΄λŠ”(subquery)λŠ” SELECT λͺ…λ Ήλ¬Έμ˜ 쑰건에 μžˆλŠ” SELECT λͺ…령문이닀. 뢀속 μ§ˆμ˜μ–΄λ₯Ό λ‹€λ₯΄κ²Œ λ§ν•˜λ©΄ λ‚΄λΆ€ 선택문이라 ν•  수 μžˆλ‹€.

​

쑰건)

1. λ§Œμ•½ 뢀속 μ§ˆμ˜μ–΄κ°€ exists μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•˜μ§€ μ•ŠλŠ”λ‹€λ©΄ selectμ ˆμ—λŠ” 였직 ν•˜λ‚˜μ˜ μˆ˜μ‹λ§Œ μ‚¬μš©λ˜μ–΄μ•Όν•œλ‹€.

2. selectμ ˆμ— distinctλ₯Ό μ‚¬μš©ν•  수 μ—†λ‹€.

3. order byμ ˆμ€ μ‚¬μš©ν•  수 μ—†λ‹€.

​

​​

​

1. μƒν˜Έ κ΄€λ ¨ 뢀속 μ§ˆμ˜μ–΄μ˜ 예제

​

예제 14-2) 학생 쀑에 λ™μ•„λ¦¬μ˜ 등급이 일반 νšŒμ›μΈ ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름, 생년월일을 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, stu_name, birthday

-> from student

-> where stu_no in

-> (select stu_no from circle where president = 2);

​

​

예제 14-3) 두 번 이상 μž₯ν•™κΈˆμ„ 지급 받은 학생 쀑 μž₯ν•™κΈˆμ•‘μ΄ ν•™κΈ°λ³„λ‘œ μ„œλ‘œ λ‹€λ₯Έ 경우의 ν•™μƒμ˜ ν•™λ²ˆμ„ 좜λ ₯ν•˜λΌ.

A)

mysql> select distinct stu_no

-> from fee f

-> where stu_no in

-> (select stu_no from fee where jang_total <> f.jang_total);

​

​

예제 14-4) 학적 ν…Œμ΄λΈ”μ—μ„œ 동아리 "Java길라작이"에 κ°€μž…ν•˜μ§€ μ•Šμ€ ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름, 주야ꡬ뢄을 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, stu_name, juya

-> from student

-> where 'Java길라작이' <> all

-> (select cir_name from circle where stu_no = student.stu_no);

​

​

예제 14-5) 학적 ν…Œμ΄λΈ”μ—μ„œ ν•™λ²ˆμ΄ κ°€μž₯ 큰 3λͺ…μ˜ ν•™λ²ˆμ„ λ‚΄λ¦Όμ°¨μˆœμœΌλ‘œ 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no

-> from student s1

-> where 3 >

-> (select count(*)

-> from student s2

-> where (s1.stu_no < s2.stu_no))

-> order by stu_no desc;

​

mysql> select stu_no from student order by stu_no desc limit 0, 3;

β€»limitλŠ” mySQL에 ν•œν•΄μ„œλ§Œ μ“°λŠ” 문법.

​

​

예제 14-6) 학적 ν…Œμ΄λΈ”μ—μ„œ ν•™λ²ˆμ΄ κ°€μž₯ μž‘μ€ ν•™λ²ˆμ„ 가진 3λͺ…μ˜ 학생을 μ˜€λ¦„μ°¨μˆœμœΌλ‘œ 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no

-> from student s1

-> where 3 >

-> (select count(*)

-> from student s2

-> where (s1.stu_no < s2.stu_no))

-> order by stu_no;

​

​

예제 14-7) λ“±λ‘ν…Œμ΄λΈ”μ—μ„œ μž₯ν•™κΈˆμ„ 지급 받은 학생 쀑 κ°€μž₯ μž‘μ€ μž₯ν•™κΈˆμ•‘μ„ 지급 받은 학생 8λͺ…μ˜ ν•™λ²ˆ, 등둝년도, 등둝학기, μž₯ν•™κΈˆ 총앑을 좜λ ₯ν•˜λΌ.

A)

mysql> select distinct stu_no, fee_year, jang_total

-> from fee f1

-> where 8 >

-> (select count(*)

-> from fee f2

-> where f1.jang_total > f2.jang_total)

-> order by f1.jang_total desc;

​

​

예제 14-8) λ“±λ‘ν…Œμ΄λΈ”μ—μ„œ λ“±λ‘ν•œ 학생 μ€‘μ—μ„œ λ‚©λΆ€ 총앑이 κ°€μž₯ 큰 학생을 ν¬ν•¨ν•œ 3λͺ…μ˜ ν•™λ²ˆ, 납뢀총앑을 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, fee_pay

-> from fee f1

-> where 3 >

-> (select count(*)

-> from fee f2

-> where f1.fee_pay < f2.fee_pay)

-> order by f1.fee_pay desc;

​

​

예제 14-9) "20161001" 학생이 κ°€μž…ν•œ 동아리에 μ†Œμ†λœ λͺ¨λ“  ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름을 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, stu_name

-> from student s

-> where not exists

-> (select * from circle c

-> where c.stu_no = '20161001'

-> and not exists

-> (select * from circle c2

-> where c.cir_name = c2.cir_name

-> and s.stu_no = c2.stu_no));

​

​

예제 14-10) 적어도 ν•œ 번 μž₯ν•™κΈˆμ„ 받은 학생에 λŒ€ν•˜μ—¬ ν•™λ²ˆ, 등둝년도, ν•™κΈ°, μž₯ν•™κΈˆμ•‘ 쀑 κ°€μž₯ 큰 μž₯ν•™κΈˆμ•‘, λ“±λ‘μΌμžλ₯Ό 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, fee_year, fee_term, jang_total, fee_date

-> from fee f1

-> where jang_total =

-> (select max(jang_total)

-> from fee f2

-> where f1.stu_no = f2.stu_no);

​

​

예제 14-11) 적어도 ν•œ 번 이상 μˆ˜κ°•μ‹ μ²­μ„ ν•˜κ³  λ“±λ‘ν•œ 학생에 λŒ€ν•˜μ—¬ ν•™λ²ˆ, 이름, 생년월일을 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, stu_name, birthday

-> from student s

-> where not exists

-> (select stu_no

-> from fee f

-> where fee_div = 'y'

-> and not exists

-> (select * from

-> attend a

-> where s.stu_no = a.stu_no

-> and a.att_div = 'y'));

​

​

​

2. λ³΅ν•©ν‚€μ˜ μ‚¬μš©

​

예제 14-14) 2016λ…„ 1학기에 λ“±λ‘ν•œ 학생이 같은 연도, 학기에 μˆ˜κ°• μ‹ μ²­ν•œ ν•™μƒμ˜ ν•™λ²ˆκ³Ό 등둝년도, 등둝학기, μˆ˜κ°•μ‹ μ²­λ…„λ„, ν•™κΈ°λ₯Ό 좜λ ₯ν•˜λΌ.(μ€‘λ³΅λœ 좜λ ₯자료λ₯Ό μ œκ±°ν•˜κΈ° μœ„ν•΄μ„œ DISTINCTλ₯Ό μ‚¬μš©)

A)

mysql> select distinct f.stu_no, fee_year, fee_term, att_year, att_term

-> from fee f, attend a

-> where f.stu_no = a.stu_no

-> and fee_year = '2016'

-> and fee_term = '1'

-> and fee_year = att_year

-> and fee_term = att_term;

​

​

​

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

​

14-3) 적어도 ν•œ 번 이상 μž₯ν•™κΈˆμ„ 받은 ν•™μƒμ˜ λ²ˆν˜Έμ™€ 이름을 좜λ ₯ν•˜λΌ. (subqueryλ₯Ό μ‚¬μš©ν•  것)

A)

mysql> select stu_no, stu_name

-> from student s

-> where exists

-> (select jang_total from fee where s.stu_no = stu_no and jang_total is not null);

​

mysql> select stu_no, stu_name

-> from student s

-> where exists

-> (select * from fee where s.stu_no = stu_no and jang_total is not null);

​

​

14-4) 적어도 ν•œ 번 이상 μˆ˜κ°• μ‹ μ²­ν•œ ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름을 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, stu_name

-> from student s

-> where exists

-> (select att_div from attend where s.stu_no = stu_no and att_div is not null);

​

mysql> select stu_no, stu_name

-> from student

-> where exists

-> (select * from attend where stu_no = student.stu_no and att_div = 'y');

​

​

14-5) μž¬ν•™μƒ μ€‘μ—μ„œ λ―Έλ“±λ‘μ΄λ©΄μ„œ λ―Έμˆ˜κ°•μžμ˜ ν•™λ²ˆ, 이름을 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no from student where stu_no not in

-> (select stu_no from fee where fee_div = 'y') and

-> stu_no not in (select stu_no from attend where att_div = 'y');

λ°˜μ‘ν˜•

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

MySQL 24λ²ˆμ§Έμˆ˜μ—…  (0) 2021.02.09
MySQL 23λ²ˆμ§Έμˆ˜μ—…  (0) 2021.02.09
MySQL 21λ²ˆμ§Έμˆ˜μ—…  (0) 2021.02.09
MySQL 20λ²ˆμ§Έμˆ˜μ—…  (0) 2021.02.09
MySQL 19λ²ˆμ§Έμˆ˜μ—…  (0) 2021.02.08

λŒ“κΈ€