๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
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

๋Œ“๊ธ€