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

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

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

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

​

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

A)

mysql> select stu_no, stu_name, mobile

-> from student

-> where mobile is not null;

​​

​

예제 9-18) νœ΄λŒ€ν°μ„ 가지고 μžˆμ§€ μ•Šμ€ ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름, νœ΄λŒ€ν° λ²ˆν˜Έκ°€ NULL인 κ²½μš°μ—λŠ” "νœ΄λŒ€ν° μ—†μŒ"을 λ‚˜νƒ€λ‚΄μ–΄λΌ.

A)

mysql> select stu_no, stu_name, ifnull(mobile, 'νœ΄λŒ€ν°μ—†μŒ')

-> from student

-> where mobile is null;

​

​​

예제 9-19) ν•™μƒμ˜ νœ΄λŒ€ν°λ²ˆν˜Έ(MOBILE)κ°€ 010이 μ•„λ‹Œ λͺ¨λ“  ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름, νœ΄λŒ€ν°λ²ˆν˜Έλ₯Ό 좜λ ₯ν•˜λΌ. (단, νœ΄λŒ€ν°μ΄ μ—†λŠ” 학생도 ν¬ν•¨λ˜μ–΄ 좜λ ₯λ˜μ–΄μ•Ό ν•œλ‹€)​

A)

mysql> select stu_no, stu_name, mobile

-> from student

-> where substring(mobile, 1, 3) <> '010'

-> or mobile is null;

​

​​

​

β–Ά λΆ€μ†μ§ˆμ˜μ–΄μ—μ„œ IN μ—°μ‚°μžβ€‹

​

예제 9-20) 등둝을 ν•œ 각 ν•™μƒμ˜ ν•™λ²ˆ, 이름을 좜λ ₯ν•˜λΌ. (inμ—°μ‚°μžλ₯Ό 이용)

A)

mysql> select stu_no, stu_name

-> from student

-> where stu_no in (20141001, 20161001, 20191004, 20191005,

-> 20191006, 20191007, 20191008, 20201002);

​​

​

예제 9-21) λΆ€μ†μ§ˆμ˜μ–΄λ₯Ό μ΄μš©ν•˜μ—¬ 등둝을 ν•œ 각 ν•™μƒμ˜ ν•™λ²ˆ, 이름을 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, stu_name

-> from student

-> where stu_no in

-> (select stu_no from fee);

​

​​

예제 9-22) 적어도 ν•œ 번의 μž₯ν•™κΈˆμ„ λ°›μ•˜λ˜ ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름을 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, stu_name

-> from student

-> where stu_no in

-> (select stu_no from fee where jang_total > 0);

​​

​

예제 9-23) "20191009"인 학생이 κ°€μž…ν•œ 동아리λ₯Ό μ œμ™Έν•œ λ‹€λ₯Έ 동아리에 적어도 ν•œ 번 κ°€μž…μ„ ν•œ ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름을 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, stu_name

-> from student

-> where stu_no in

-> (select stu_no from circle where cir_name not in

-> (select cir_name from circle where stu_no = '20191009'));

​​

​

예제 9-24) νœ΄λŒ€ν°μ„ 가지고 μžˆλŠ” 학생을 좜λ ₯ν•˜λΌ. (단, νœ΄λŒ€ν°μ΄ μžˆμ–΄λ„ 야간인 학생은 μ œμ™Έν•œλ‹€)

A)

mysql> select stu_no, stu_name, mobile

-> from student

-> where mobile not in

-> (select mobile from student where juya='μ•Ό');

​

​

​​

β–Ά 뢀속 μ§ˆμ˜μ–΄μ—μ„œ 관계 μ—°μ‚°μž

​​

예제 9-25) μˆ˜κ°•μ‹ μ²­μ„ ν•œ ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름을 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, stu_name

-> from student

-> where stu_no in

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

​​

​

예제 9-26) μž₯수인(1999년생) 보닀 λ‚˜μ΄κ°€ 더 λ§Žμ€ 각 ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름, 생년월일을 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, stu_name, birthday

-> from student

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

-> (select substring(birthday, 1, 4)

-> from​ student

-> where stu_name = 'μž₯수인');

​

​

​

β–Ά ALLκ³Ό ANY μ—°μ‚°μž

​

-ANY μ—°μ‚°μž

> ANY : μ΅œμ†Œκ°’ 보닀 크면

>= ANY : μ΅œμ†Œκ°’λ³΄λ‹€ ν¬κ±°λ‚˜ κ°™μœΌλ©΄

< ANY : μ΅œλŒ€κ°’λ³΄λ‹€ μž‘μœΌλ©΄

<= ANY : μ΅œλŒ€κ°’λ³΄λ‹€ μž‘κ±°λ‚˜ κ°™μœΌλ©΄

= ANY : INκ³Ό 같은 효과

!= ANY : NOT INκ³Ό 같은 효과

​

-ALL μ—°μ‚°μž

> ALL : μ΅œλŒ€κ°’ 보닀 크면

>= ALL : μ΅œλŒ€κ°’λ³΄λ‹€ ν¬κ±°λ‚˜ κ°™μœΌλ©΄

< ALL : μ΅œμ†Œκ°’λ³΄λ‹€ μž‘μœΌλ©΄

<= ALL : μ΅œμ†Œκ°’λ³΄λ‹€ μž‘κ±°λ‚˜ κ°™μœΌλ©΄

= ALL : SUBSELECT의 κ²°κ³Όκ°€ 1건이면 μƒκ΄€μ—†μ§€λ§Œ μ—¬λŸ¬ 건이면 였λ₯˜κ°€ λ°œμƒν•œλ‹€.

!= ALL : μœ„μ™€ λ§ˆμ°¬κ°€μ§€λ‘œ SUBSELECT의 κ²°κ³Όκ°€ μ—¬λŸ¬ 건이면 였λ₯˜κ°€ λ°œμƒν•œλ‹€.

​​

​

예제 9-27) κ°€μž₯ λ‚˜μ΄κ°€ λ§Žμ€ ν•™μƒμ˜ ν•™λ²ˆ, 이름, 생년월일을 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, stu_name, birthday

-> from student

-> where birthday <= all

-> (select birthday from student);

​

​

예제 9-28) κ°€μž₯ λ‚˜μ΄κ°€ λ§Žμ€ 학생(박도상)을 μ œμ™Έν•œ λ‚˜λ¨Έμ§€ λͺ¨λ“  ν•™μƒμ˜ ν•™λ²ˆ, 이름, 생년월일을 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, stu_name, birthday

-> from student

-> where birthday > any

-> (select birthday from student);

​

​

예제 9-29) ν•™λ²ˆ 20191004인 학생이 λ“±λ‘ν•œ λ“±λ‘κΈˆμ˜ 납뢀총앑(1,000,000)보닀 더 λ§Žμ€ λ“±λ‘κΈˆμ„ λ‚Έ ν•™μƒμ˜ ν•™λ²ˆκ³Ό 납뢀총앑을 좜λ ₯ν•˜λΌ. μ΄λ•Œ 20191004λŠ” κ²°κ³Όμ—μ„œ μ œμ™Έν•œλ‹€.

A)

mysql> select distinct stu_no, fee_pay

-> from fee

-> where stu_no <> '20191004'

-> and fee_pay > any

-> (select fee_pay from fee where stu_no = '20191004');

​

​

​

β–Ά EXISTS μ—°μ‚°μžβ€‹

​

예제 9-30) 등둝을 ν•œ ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름을 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, stu_name

-> from student

-> where stu_no in

-> (select stu_no from fee);

​​

​

예제 9-31) λ“±λ‘ν•˜μ§€ μ•Šμ€ ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름을 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, stu_name

-> from student

-> where not exists

-> (select * from fee where stu_no = student.stu_no);

​

​

예제 9-32) 학적 ν…Œμ΄λΈ”μ—μ„œ ν•™λ²ˆ, 이름, νœ΄λŒ€ν°λ²ˆν˜Έ, 우편번호λ₯Ό 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, stu_name, mobile, post_no from student;

​

​

예제 9-33) 각각의 λ„μ‹œμ— κ±°μ£Όν•˜λŠ” λͺ¨λ“  학생에 λŒ€ν•˜μ—¬ νœ΄λŒ€ν°μ„ 가지고 μžˆλŠ” ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름, 우편번호, νœ΄λŒ€ν°λ²ˆν˜Έλ₯Ό λ‚˜νƒ€λ‚΄μ–΄λΌ. (단, νœ΄λŒ€ν°μ΄ μžˆλŠ” 학생과 νœ΄λŒ€ν°μ΄ μ—†λŠ” ν•™μƒμ˜ 우편번호 μ•ž 1μžλ¦¬κ°€ λ™μΌν•œ 학생은 μ œμ™Έμ‹œν‚¨λ‹€.)

A)

mysql> select stu_no, stu_name, post_no, mobile

-> from student s1

-> where not mobile in

-> (select mobile from student s2

-> where substring(s1.post_no, 1, 1) = substring(s2.post_no, 1, 1)

-> and mobile is null);

​

​

​

β–Ά λΆ€μ • 쑰건

​

예제 9-34) "Java길라작이" 동아리에 κ°€μž…ν•œ ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름을 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, stu_name

-> from circle

-> where cir_name = 'Java길라작이';

​

​

예제 9-35) "Java길라작이" 동아리에 κ°€μž…ν•˜μ§€ μ•Šμ€ ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름을 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, stu_name, cir_name

-> from circle

-> where cir_name <> 'Java길라작이';

​

​

예제 9-36) 등둝 ν…Œμ΄λΈ”μ—μ„œ μž₯ν•™ μ½”λ“œκ°€ '11' ν•™μƒμ˜ ν•™λ²ˆκ³Ό μž₯ν•™μ½”λ“œ, μž₯ν•™κΈˆ 총앑을 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, jang_code, jang_total

-> from fee

-> where jang_code = 11;

​

​

예제 9-37) λ“±λ‘ν…Œμ΄λΈ”μ—μ„œ μž₯ν•™μ½”λ“œκ°€ '11'이 μ•„λ‹Œ ν•™μƒμ˜ ν•™λ²ˆκ³Ό μž₯ν•™μ½”λ“œ, μž₯ν•™κΈˆ 총앑을 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, jang_code, jang_total

-> from fee

-> where jang_code <> 11;

​

​

예제 9-38) λ“±λ‘ν…Œμ΄λΈ”μ—μ„œ μž₯ν•™μ½”λ“œκ°€ '11'이 μ•„λ‹Œ ν•™μƒμ˜ ν•™λ²ˆκ³Ό, μž₯ν•™μ½”λ“œ, μž₯ν•™κΈˆ 총앑을 좜λ ₯ν•˜λΌ. (NOT IN 이용)

A)

mysql> select stu_no, jang_code, jang_total

-> from fee

-> where jang_code not in

-> (select jang_code from fee where jang_code in (11));

​

​

예제 9-39) λ“±λ‘ν…Œμ΄λΈ”μ—μ„œ μž₯ν•™μ½”λ“œκ°€ '11'이 μ•„λ‹Œ ν•™μƒμ˜ ν•™λ²ˆκ³Ό μž₯ν•™μ½”λ“œ, μž₯ν•™κΈˆ 총앑을 좜λ ₯ν•˜λΌ.

(단, NOT IN μ΄μš©ν•˜κ³  μž₯ν•™μ½”λ“œκ°€ NULL인 학생도 ν¬ν•¨ν•˜μ—¬ 좜λ ₯ν•˜λΌ.)

A)

mysql> select stu_no, jang_code, jang_total

-> from fee

-> where jang_code not in

-> (select jang_code from fee where jang_code in (11))

-> or jang_code is null;

​

​

​​

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

 

9-1) 2000λ…„ 이후에 μΆœμƒν•œ 각 ν•™μƒμ˜ ν•™λ²ˆμ„ λ‚˜νƒ€λ‚΄μ–΄λΌ.

A)

mysql> select stu_no, birthday from student where substring(birthday, 1, 4) > 2000;

​

​

9-2) ν•™λ²ˆ 20141001번이 λ“±λ‘ν•˜μ˜€λ‹€λ©΄ ν•™μƒμ˜ 이름을 λ‚˜νƒ€λ‚΄μ–΄λΌ.

A)

mysql> select stu_name from student where stu_no = '20141001';

​

​

9-4) 적어도 ν•œ 번 이상 μž₯ν•™κΈˆμ„ 받은 ν•™μƒμ˜ ν•™λ²ˆμ„ 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no

-> from student

-> where stu_no in

-> (select stu_no from fee where jang_total > 0);

​

​

9-5) 1,500,000원 이상 μž₯ν•™κΈˆμ„ 받은 ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름을 좜λ ₯ν•˜λΌ.

A)

mysql> select distinct s.stu_no, stu_name, jang_total

-> from student s, fee f

-> where s.stu_no = f.stu_no and jang_total >= 1500000;

​

​

9-6) 성별이 λ‚¨μžκ°€ μ•„λ‹Œ ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름을 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, stu_name

-> from student

-> where (gender % 2 = 0);

​

​

9-7) 1998λ…„λΆ€ν„° 2001λ…„ 사이에 μΆœμƒν•œ ν•™μƒμ˜ ν•™λ²ˆκ³Ό 이름, μΆœμƒλ…„λ„λ₯Ό 좜λ ₯ν•˜λΌ.

A)

mysql> select stu_no, stu_name, substring(birthday, 1, 4) μΆœμƒλ…„λ„

-> from student

-> where substring(birthday, 1, 4) between 1998 and 2001;

​

​

9-8) 적어도 ν•œ λ²ˆμ€ μž₯ν•™κΈˆμ„ λ°›κ³  1999λ…„ 이후에 μΆœμƒν•œ ν•™μƒμ˜ ν•™λ²ˆ, 이름, μž₯ν•™κΈˆ 총앑을 좜λ ₯ν•˜λΌ.

A)

mysql> select s.stu_no, stu_name, jang_total

-> from student s, fee f

-> where s.stu_no = f.stu_no and jang_total is not null

-> and substring(birthday, 1, 4) > 1999;

​

​

9-10) μž₯ν•™κΈˆ 수령 총앑이 500,000μ›μ—μ„œ 2,000,000원 사이에 ν¬ν•¨λ˜λŠ” 각 ν•™μƒμ˜ ν•™λ²ˆμ„ 좜λ ₯ν•˜λΌ.

A)

mysql> select distinct stu_no from fee where jang_total between 500000 and 2000000;

 

λ°˜μ‘ν˜•

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

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

λŒ“κΈ€