βΆ 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 |
λκΈ