Day13. MySQL_3

박경희·2023년 6월 12일
0

강의 복습 및 정리

목록 보기
19/38

수업 후 바로 미니 프로젝트를 준비하느라 다소 늦었지만 오늘 다시 살펴보며 복습했다_2
1. like

select *
from student1
where name like "%골%"			//앞, 뒤 상관없이 '골'이 들어간걸 찾겠다.
;

select *
from student1
where name like "골%"			//'골'로 시작하는 것을 찾겠다.
;

select *
from student1
where name like "%골"			//'골'로 끝나는 것을 찾겠다.
;

select *
from student1
where name like "%골_"			//'골'이 들어간 두 글자를 찾겠다.
;

select *
from student1
where name like "%골__"			//'골'이 들어간 세 글자를 찾겠다.
;
  1. 재학생 그룹이 있는 상태에서 졸업생 그룹 생성.
create table student1_graduate(
    id int ,
    name varchar(255),
    create_at datetime,
    graduate_date datetime default now(),
    primary key(id)
);
// 2-1. 졸업생그룹에 졸업생 넣기
select * from student1_graduate;
select * from student1 where name = "골론즈";
insert into student1_graduate(id, name, create_at)
(select id, name, create_at from student1 where name = "골론즈");
// 2-2.재학생에서 골론즈 없애기
delete from student1
where name = "골론즈";

-> 이렇게 하면 재학생에 '골론즈'가 3명 있다면 3명이 모두 사라져버리게 된다. 그래서 id로 지워야 정확히 지우고 싶은 '골론즈'를 지울 수 있다.

delete from student1
where id = 
(select * from (select id from student1 where name = "골론즈") as f limit 1);
  1. union : 위 테이블과 아래 테이블을 한번에 보고 싶을 때 사용. 컬럼을 맞춰줘야 한다.
elect id, name from student1
union all
select id, name from student1_graduate
;

ex)

select name, id from user
union
select name, id from product
union
select make_time from product; --> 이건 컬럼 개수가 맞지 않는 것.
select make_time, id from product; --> 이렇게 개수를 맞춰줘야 된다.
  • union은 중복되면 빼주고
    union all 은 중복되는 것까지 모두 출력해준다.
  1. order by는 정렬하기. 뒤에 desc를 붙이면 내림차순 정렬이 된다.

  2. 프래그래머스 - '주문량이 많은 아이스크림들 조회하기' 를 풀어보았다~
    나중에 다시 풀어봐보쟝

select FLAVOR
from
(
SELECT SHIPMENT_ID, FLAVOR, TOTAL_ORDER from FIRST_HALF
union
SELECT SHIPMENT_ID, FLAVOR, TOTAL_ORDER from JULY
) as t

group by FLAVOR
order by sum(TOTAL_ORDER) desc
limit 3;
  1. JDBC
  • JAVA DATA BASE CONNECT (자바와 데이터베이스 연결)

  • java 프로그램을 만들었고, 데이터도 만들었는데 작업창을 끄면 다 날라감.
    이게 날라가지 않고 영구적? 으로 저장하려고 DB에 저장하는것이다.

0개의 댓글