โถ ๋ถ์ ์ง์์ด
๋ถ์ ์ง์์ด๋(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 |
๋๊ธ