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

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

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

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

​

11-10) λ“±λ‘κΈˆ 총앑이 20191005번과 λ™μΌν•œ ν•™μƒμ˜ ν•™λ²ˆκ³Ό λ“±λ‘κΈˆμ„ 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, sum(fee_total)

-> from fee

-> group by stu_no

-> having sum(fee_total) = all

-> (select sum(fee_total) from fee where stu_no = '20191005');

​

​

11-11) μˆ˜κ°•μ‹ μ²­ν•œ 학생이 등둝을 ν•˜μ˜€λ‹€λ©΄ 학생이 λ‚©μž…ν•œ ν•™λ²ˆκ³Ό λ“±λ‘κΈˆ 총앑을 좜λ ₯ν•˜λΌ.​

A)

mysql> select stu_no, sum(fee_total)

-> from fee

-> group by stu_no

-> having stu_no in

-> (select stu_no from attend where att_div = 'Y');

​

​

11-12) 동아리 회μž₯인 학생이 등둝을 ν•˜μ˜€μœΌλ©΄ ν•™λ²ˆ, 이름, 등둝년도, 등둝학기, λ“±λ‘μ²˜λ¦¬μΌμžλ₯Ό 좜λ ₯ν•˜λΌ.

A)

mysql> select f.stu_no, stu_name, fee_year, fee_term, fee_date

-> from student s, fee f

-> where s.stu_no = f.stu_no and

-> f.stu_no in (select stu_no from circle where president = 0);

​​​

​

​

β–Ά SELECT λͺ…λ Ήλ¬Έ : ORDER BY절

​

1. μ—΄μ˜ κ·Έλ£Ήν™”

​

예제 12-1) λ“±λ‘ν•œ 학생 쀑 λ“±λ‘μΌμžκ°€ 2019에 ν•΄λ‹Ήν•˜λŠ” ν•™μƒμ˜ ν•™λ²ˆκ³Ό λ“±λ‘μΌμžλ₯Ό 좜λ ₯ν•˜λΌ. 이 λ•Œ μ •λ ¬ μˆœμ„œλŠ” ν•™λ²ˆμœΌλ‘œ ν•œλ‹€.

A)

mysql> select stu_no, fee_year, fee_term, fee_date

-> from fee

-> where year(fee_date) = 2019

-> order by stu_no;

​

​

​

2. μˆœμ„œ 번호둜 μ •λ ¬

​

예제 12-2) λ“±λ‘ν•œ 학생에 λŒ€ν•˜μ—¬ ν•™λ²ˆκ³Ό 전체 λ“±λ‘κΈˆμ˜ 총앑을 좜λ ₯ν•˜λΌ. 이 λ•Œ 좜λ ₯ μˆœμ„œλŠ” λ“±λ‘κΈˆ 총앑이닀.

A)

mysql> select stu_no, sum(fee_total)

-> from fee

-> group by stu_no

-> order by 2;

​

​

​

3. μ˜€λ¦„μ°¨μˆœκ³Ό λ‚΄λ¦Όμ°¨μˆœ μ •λ ¬

​

예제 12-3) 학적 ν…Œμ΄λΈ”μ˜ μ˜λ¬Έμ΄λ¦„μ˜ 첫 번째 λ¬Έμžκ°€ 'J'보닀 큰 ASCII μ½”λ“œλ‘œ μ‹œμž‘ν•˜λŠ” ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름, μ˜λ¬Έμ΄λ¦„μ„ 좜λ ₯ν•˜λΌ.(단, μ˜λ¬Έμ΄λ¦„μ€ λ‚΄λ¦Όμ°¨μˆœμœΌλ‘œ μ •λ ¬ν•œλ‹€)

A)

mysql> select stu_no, stu_name, stu_ename

-> from student

-> where substring(stu_ename, 1, 1) > 'K'

-> order by stu_ename desc;

​​

​

​

4. ν•˜λ‚˜ μ΄μƒμ˜ ν–‰ μ •λ ¬

​

예제 12-4) 2019년에 λ“±λ‘ν•œ 학생에 λŒ€ν•˜μ—¬ ν•™λ²ˆκ³Ό λ‚©μž…ν•œ λ“±λ‘κΈˆ 총앑을 좜λ ₯ν•˜λΌ. 이 λ•Œ 좜λ ₯ μˆœμ„œλŠ” ν•™λ²ˆμ€ μ˜€λ¦„μ°¨μˆœ, λ“±λ‘κΈˆ 총앑은 λ‚΄λ¦Όμ°¨μˆœμ΄λ‹€.

A)

mysql> select stu_no, fee_year, fee_term, fee_pay

-> from fee

-> where fee_year = 2019

-> order by stu_no asc, fee_pay desc;

​

​

예제 12-5) ꡐ과λͺ© ν…Œμ΄λΈ”μ—μ„œ 생성년도가 2003λ…„~2005λ…„ 사이 κ°’μœΌλ‘œ κ³Όλͺ©μ½”λ“œ, κ³Όλͺ©λͺ…, 생성년도λ₯Ό 좜λ ₯ν•˜λΌ. (단, 좜λ ₯μˆœμ„œλŠ” 생성년도 λ‚΄λ¦Όμ°¨μˆœμœΌλ‘œ μ •λ ¬ν•œ λ’€ 같은 생성년도인 κ²½μš°λŠ” κ³Όλͺ© μ½”λ“œ 순으둜 μ •λ ¬)

A)

mysql> select sub_code, sub_name, create_year

-> from subject

-> where create_year between 2003 and 2005

-> order by create_year desc, sub_code;

​

​

예제 12-6) 성적 ν…Œμ΄λΈ”μ—μ„œ 2019년도 1ν•™κΈ° μ„μ°¨μˆœ λͺ…λΆ€λ₯Ό ν•™λ²ˆ, 연도, ν•™κΈ°, 평점평균, 총점을 좜λ ₯ν•˜λΌ.(단, 좜λ ₯μˆœμ„œλŠ” 평점평균 λ‚΄λ¦Όμ°¨μˆœ, 총점 λ‚΄λ¦Όμ°¨μˆœ, 동점인 κ²½μš°μ—λŠ” ν•™λ²ˆμˆœμ„œλ‘œ μ •λ ¬ν•œλ‹€)

A)

mysql> select stu_no, sco_year, sco_term, exam_avg, exam_total

-> from score

-> where sco_year = '2019' and sco_term = 1

-> order by exam_avg desc, exam_total desc, stu_no;

​​

​

​

5. μˆ˜μ‹κ³Ό ν•¨μˆ˜μ˜ μ •λ ¬

​

예제 12-7) ꡐ수 ν…Œμ΄λΈ”μ—μ„œ μž„μš©μΌμžκ°€ 1997λ…„ 이전인 κ΅μˆ˜μ½”λ“œ, κ΅μˆ˜μ΄λ¦„, μž„μš©μΌμžλ₯Ό κ΅μˆ˜μž„μš©μΌμž 순으둜 μ •λ ¬ν•˜λΌ.(DATE_FORMAT( ) 힘수λ₯Ό μ΄μš©ν•˜λΌ)

A)

mysql> select prof_code, prof_name, create_date "μž„μš©μΌμž"

-> from professor

-> where date_format(create_date, '%Y') < 1997

-> order by date_format(create_date, '%Y');

​​

​

​

6. NULL κ°’μ˜ μ •λ ¬

​

예제 12-8) λ“±λ‘ν…Œμ΄λΈ”μ—μ„œ 2008λ…„κ³Ό 2019년에 λ“±λ‘ν•œ ν•™μƒμ˜ ν•™λ²ˆκ³Ό μž₯ν•™κΈˆ 총앑, λ“±λ‘μΌμžλ₯Ό 좜λ ₯ν•˜λΌ. 단, μˆœμ„œλŠ” μž₯ν•™κΈˆ 총앑 μ˜€λ¦„μ°¨μˆœμ΄λ‹€.

A)

mysql> select stu_no, jang_total, fee_date

-> from fee

-> where year(fee_date) = 2008 or year(fee_date) = 2019

-> order by jang_total;

​

​

예제 12-9) λ“±λ‘ν…Œμ΄λΈ”μ—μ„œ 2008λ…„κ³Ό 2019년에 λ“±λ‘ν•œ ν•™μƒμ˜ ν•™λ²ˆκ³Ό μž₯ν•™κΈˆ 총앑, λ“±λ‘μΌμžλ₯Ό 좜λ ₯ν•˜λΌ. 단, μˆœμ„œλŠ” μž₯ν•™κΈˆ 총앑 λ‚΄λ¦Όμ°¨μˆœμ΄λ‹€.

A)

mysql> select stu_no, jang_total, fee_date

-> from fee

-> where year(fee_date) = 2008 or year(fee_date) = 2019

-> order by jang_total desc;

​

​

​

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

​

12-1) STUDENT ν…Œμ΄λΈ”μ—μ„œ ν•™λ²ˆ, 이름, ν•™κ³Όμ½”λ“œ, 학년을 좜λ ₯ν•˜λΌ. 단 학과별, 학년별, ν•™λ²ˆμˆœμœΌλ‘œ μ •λ ¬ν•˜λΌ.

A)

mysql> select stu_no, stu_name, dept_code, grade

-> from student

-> order by dept_code, grade, stu_no;

​

​

12-3) λ“±λ‘ν…Œμ΄λΈ”μ—μ„œ λ‚©μž…ν•œ 총앑은 λ“±λ‘κΈˆ μ΄μ•‘μ—μ„œ μž₯ν•™κΈˆ 총앑을 κ°μ‚°ν•œ 결과이닀. 이 λ•Œ μ΅œμ’… κ²°κ³ΌλŠ” 감산을 μˆ˜ν–‰ν•œ κ°’μ˜ μˆœμ„œλ‘œ μ •λ ¬ν•˜λΌ.

A)

mysql> select stu_no, fee_total, jang_total, fee_total-ifnull(jang_total, 0)

-> from fee

-> order by fee_total-ifnull(jang_total, 0);

​​

​

​

β–ΆSELECT λͺ…λ Ήλ¬Έμ˜ μ‘°ν•©

​

1. UNION의 μ‘°ν•©

​

예제 13-1) 학년이 2, 4학년인 ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름을 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, stu_name, grade

-> from student

-> where grade = 2

-> union

-> select stu_no, stu_name, grade

-> from student

-> where grade = 4;

​

​

예제 13-3) 적어도 ν•œ 번 이상 μˆ˜κ°•μ‹ μ²­μ„ ν–ˆκ±°λ‚˜ 등둝을 ν•œ ν•™μƒμ˜ ν•™λ²ˆμ„ 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no from attend

-> union

-> select stu_no from fee;

​

​

예제 13-4) 적어도 ν•œ 번 동아리에 κ°€μž…ν–ˆκ±°λ‚˜, 그리고 등둝을 ν•˜κ³  μˆ˜κ°•μ‹ μ²­μ„ ν•œ 학생 μ€‘μ—μ„œ μ΄μƒμ˜ 쑰건 쀑 λ‘˜ λ˜λŠ” 세가지 쑰건을 λ§Œμ‘±ν•˜λŠ” λ˜λŠ” λ‘κ°œμ˜ 쑰건에 λͺ¨λ‘ ν¬ν•¨ν•˜λŠ” 각 ν•™μƒμ˜ ν•™λ²ˆμ„ 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no from circle

-> union

-> select stu_no from fee

-> union

-> select stu_no from attend where att_div = 'Y';

​​

​

​

2. UNION ALL의 μ‘°ν•©

​

예제 13-5) 적어도 ν•œ 번 이상 λ“±λ‘κΈˆ λ‚©λΆ€ν•˜κ³  동아리에 κ°€μž…ν•œ λͺ¨λ“  학생을 좜λ ₯ν•˜λΌ. (단, μ€‘λ³΅λœ 행을 μ œκ±°ν•˜μ§€ μ•Šκ³  λͺ¨λ‘ 좜λ ₯ν•œλ‹€)

A)

mysql> select stu_no from fee

-> union all

-> select stu_no from circle;

​

​

​​

3. 집합 μ—°μ‚°μžμ™€ NULL κ°’

​

예제 13-6) 20161001 ν•™μƒμ˜ ν•™λ²ˆ, 등둝년도, ν•™κΈ°, μž₯ν•™κΈˆμ•‘μ„ 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, fee_year, fee_term, jang_total

-> from fee

-> where stu_no = '20161001';

​

​

예제 13-7) 20161001 학생과 20201002 학생이 받은 μž₯ν•™κΈˆμ•‘μ„ 좜λ ₯ν•˜λΌ.

A)

mysql> select​ stu_no, jang_total

-> from fee

-> where stu_no = '20161001'

-> union

-> select stu_no, jang_total

-> from fee

-> where stu_no = '20201002';

​

mysql> select stu_no, jang_total, fee_year, fee_term

-> from fee

-> where stu_no = '20161001'

-> union

-> select stu_no, jang_total, fee_year, fee_term

-> from fee

-> where stu_no = '20201002';

​

​

예제 13-8) 20161001 학생과 20191008 학생이 받은 μž₯ν•™κΈˆμ•‘μ„ 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, jang_total

-> from fee

-> where stu_no = '20161001'

-> union

-> select stu_no, jang_total

-> from fee

-> where stu_no = '20191008';

 

λ°˜μ‘ν˜•

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

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

λŒ“κΈ€