βΆ λΆμ μ§μμ΄
λΆμ μ§μμ΄λ(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 |
λκΈ