<μ°μ΅λ¬Έμ >
β
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 |
λκΈ