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

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

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

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

 

8-3) 각 학생에 λŒ€ν•˜μ—¬ μˆ˜κ°•μ‹ μ²­ κ³Όλͺ©μ½”λ“œ, μˆ˜κ°•ν•™μ μ„ λ‚˜νƒ€λ‚΄μ–΄λΌ.

A)

mysql> select stu_no, sub_code, att_point from attend where att_div = 'y';

​

​

​

8-4) μž₯ν•™κΈˆμ„ 받은 ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름을 λ‚˜νƒ€λ‚΄μ–΄λΌ.

A)

mysql> select distinct​ s.stu_no, stu_name

→ from student s, fee f​

→ where s.stu_no = f.stu​_no and ifnull(jang_total, 0) > 0;

​

​

​

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

​

1. 관계 μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•˜λŠ” 쑰건

​​

예제 9-1) 성별이 μ—¬μžμΈ ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름, 성별, 생년월일을 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, stu_name, gender, birthday

-> from student

-> where gender = 2 or gender = 4 or gender = 6;

​

​

예제 9-2) μ•Όκ°„ ν•™μƒλ“€μ˜ ν•™λ²ˆκ³Ό 이름을 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, stu_name

-> from student

-> where juya = 'μ•Ό';

​

​

예제 9-3) ν•™λ²ˆμ΄ 20191008인 ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름을 λ‚˜νƒ€λ‚΄μ–΄λΌ.

A)

mysql> select stu_no, stu_name

-> from student

-> where stu_no = '20191008';

​

​

예제 9-4) νœ΄λŒ€ν°μ„ κ°€μ§€κ³ μžˆλŠ” ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름, νœ΄λŒ€ν°λ²ˆν˜Έλ₯Ό λ‚˜νƒ€λ‚΄μ–΄λΌ.

A)

mysql> select stu_no, stu_name, mobile

-> from student

-> where mobile = mobile;

-μ—¬κΈ°μ„œ mobile = mobile은 λ™μΌν•œ mobile이 μ‘΄μž¬ν•˜λ©΄ λͺ¨λ“  행이 좜λ ₯될 것이고 mobile이 NULL값인 κ²½μš°μ—λŠ” 좜λ ₯λ˜μ§€ μ•ŠλŠ”λ‹€.

​​

​

예제 9-5) 2000λ…„ 이전에 μΆœμƒν•œ μ—¬ν•™μƒμ˜ ν•™λ²ˆ, 이름, 생년월일을 좜λ ₯ν•˜λΌ.

A)

1.

mysql> select stu_no, stu_name, gender, birthday

-> from student

-> where gender = 2 and substring(birthday, 1, 4) < 2000;

​

2.

mysql> select stu_no, stu_name, gender, birthday

-> from student

-> where (gender = 2 or gender = 4 or gender = 6) and substring(birthday, 1, 4) < 2000;

β˜…μ—°μ‚° μš°μ„ μˆœμœ„

1. and

2. or

κ·ΈλŸ¬λ―€λ‘œ κΌ­ κ΄„ν˜Έλ‘œ λ¬Άμ–΄μ£Όμž.

​

​​

예제 9-6) 경인지역에 κ±°μ£Όν•˜λŠ” ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름, 집 μ „ν™”λ²ˆν˜Έλ₯Ό λ‚˜νƒ€λ‚΄μ–΄λΌ. (단, 집 μ „ν™”μ˜ μ§€μ—­λ²ˆν˜Έ 02λŠ” μ„œμšΈ 거주자이고 031은 경기도 κ±°μ£Όμžμ΄λ‹€. 집전화가 μ—†λŠ” κ²½μš°λŠ” μ œμ™Έν•œλ‹€.)

A)

mysql> select stu_no, stu_name, concat(tel1, '-', tel2, '-', tel3) "집전화"

-> from student

-> where tel1 = '02' or tel1 = '031';

​

​

예제 9-7) 2000년도에 νƒœμ–΄λ‚˜μ§€ μ•Šμ€ ν•™μƒμ˜ ν•™λ²ˆ, 이름, 생년월일을 λ‚˜νƒ€λ‚΄μ–΄λΌ.​

A)

mysql> select stu_no, stu_name, birthday

-> from student

-> where substring(birthday, 1, 4) <> 2000;

- <> : 같지 μ•Šλ‹€λŠ” 뜻

​​

​

예제 9-8) 성별이 λ‚¨μžμ΄κ±°λ‚˜ 1999년에 μΆœμƒν•œ ν•™μƒμ˜ ν•™λ²ˆ, 이름, 성별, 생년월일을 λ‚˜νƒ€λ‚΄μ–΄λΌ. κ·ΈλŸ¬λ‚˜ 1999년도에 μΆœμƒν•œ 남학생은 μ œμ™Έν•œλ‹€.

A)

mysql> select stu_no, stu_name, gender, birthday from student

-> where (gender = 1 or substring(birthday, 1, 4) = 1999)

-> and​ not (gender = 1 and substring(birthday, 1, 4) = 1999);

​

​

​

β–Ά BETWEEN μ—°μ‚°μž

​

예제 9-9) ν˜„μž¬ λ‚˜μ΄κ°€ 20μ‚΄λΆ€ν„° 23μ‚΄κΉŒμ§€ ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름, λ‚˜μ΄λ₯Ό 좜λ ₯ν•˜λΌ. (단, AGES λ·°ν…Œμ΄λΈ”μ„ μ‚¬μš©ν•œλ‹€.)

A)

1.

mysql> select stu_no, stu_name, age

-> from ages

-> where age >= 20

-> and age <= 23;

​

2.

mysql> select stu_no, stu_name, age

-> from ages

-> where age between 20 and 23;

​

​

예제 9-10) μΆœμƒμ—°λ„κ°€ 1997λ…„λΆ€ν„° 2000λ…„ 사이에 νƒœμ–΄λ‚œ ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름, μΆœμƒλ…„λ„λ₯Ό 좜λ ₯ν•˜λΌ. 단, μΆœμƒλ…„λ„ μ˜€λ¦„μ°¨μˆœμœΌλ‘œ 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, stu_name, birthday

-> from student

-> where substring(birthday, 1, 4) between 1997 and 2000

-> order by birthday;

​

​

​

β–Ά IN μ—°μ‚°μž

​

예제 9-11) μš°νŽΈλ²ˆν˜Έκ°€ 01066, 01901, 06305에 ν•΄λ‹Ήλ˜λŠ” 각 ν•™μƒμ˜ ν•™λ²ˆ, 이름, 우편번호λ₯Ό 좜λ ₯ν•˜λΌ.

​A)

1.

mysql> select stu_no, stu_name, post_no

-> from student

-> where post_no = '01066'

-> or post_no = '01901'

-> or post_no = '06305';

​

2.

mysql> select stu_no, stu_name, post_no

-> from student

-> where post_no in('01066', '01901', '06305');

​

​

예제 9-12) 1996, 1997, 2001년에 μΆœμƒν•œ 각 ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름, 생년월일을 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, stu_name, birthday

-> from student

-> where substring(birthday, 1, 4) in(1996, 1997, 2001);

 

​

​

​

β–Ά LIKE μ—°μ‚°μž

LIKE μ—°μ‚°μžλŠ” νŠΉλ³„ν•œ νŒ¨ν„΄μ΄λ‚˜ 마슀크λ₯Ό κ°€μ§€λŠ” 영수치 값을 선택할 λ•Œ μ‚¬μš©λœλ‹€.​

​

예제 9-13) μ˜λ¬Έμ΄λ¦„μ΄ 문자 K둜 μ‹œμž‘ν•˜λŠ” ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름, μ˜λ¬Έμ΄λ¦„μ„ λ‚˜νƒ€λ‚΄μ–΄λΌ.

A)

mysql> select stu_no, stu_name, stu_ename

-> from student

-> where stu_ename like 'K%';

​

​

예제 9-14) μ˜λ¬Έμ΄λ¦„μ˜ 맨 끝에 문자 gλ₯Ό 가지고 μžˆλŠ” ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름을 λ‚˜νƒ€λ‚΄μ–΄λΌ.

A)

mysql> select stu_no, stu_ename

-> from student

-> where stu_ename like '%g';

 

-νŠΉλ³„ν•œ 의미λ₯Ό λ‚˜νƒ€λ‚΄λŠ” 또 λ‹€λ₯Έ κΈ°ν˜ΈλŠ” 밑쀄 기호(_) 이닀. 밑쀄 κΈ°ν˜ΈλŠ” LIKE μ—°μ‚°μžμ™€ 같이 μ‚¬μš©λ˜λ©΄ μ •ν™•νžˆ ν•œ 문자λ₯Ό λ‚˜νƒ€λ‚Έλ‹€.

​

​​

예제 9-15) μ˜λ¬Έμ΄λ¦„μ˜ λμ—μ„œ λ‘λ²ˆμ§Έ λ¬Έμžκ°€ i인 ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름을 λ‚˜νƒ€λ‚΄μ–΄λΌ.

A)

mysql> select stu_no, stu_ename

-> from student

-> where stu_ename like '%i_';

​

​

예제 9-16) μ˜λ¬Έμ΄λ¦„μ΄ 문자 K둜 μ‹œμž‘ν•˜μ§€μ•ŠλŠ” ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름을 λ‚˜νƒ€λ‚΄μ–΄λΌ.

A)

mysql> select stu_no, stu_ename

-> from student

-> where not (stu_ename like 'K%');

 

λ°˜μ‘ν˜•

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

MySQL 18λ²ˆμ§Έμˆ˜μ—…  (0) 2021.02.08
MySQL 17λ²ˆμ§Έμˆ˜μ—…  (0) 2021.02.08
MySQL 15λ²ˆμ§Έμˆ˜μ—…  (0) 2021.02.08
MySQL 14λ²ˆμ§Έμˆ˜μ—…  (0) 2021.02.08
MySQL 13λ²ˆμ§Έμˆ˜μ—…  (0) 2021.02.08

λŒ“κΈ€