[MariaDB Tutorial 번역 글]
- Select : https://rural-mouse.tistory.com/43
- Order By : https://rural-mouse.tistory.com/44
- Where : https://rural-mouse.tistory.com/45
- Distinct : https://rural-mouse.tistory.com/46
- Between : https://rural-mouse.tistory.com/48
- Like : https://rural-mouse.tistory.com/49
- In : https://rural-mouse.tistory.com/50
- Limit : https://rural-mouse.tistory.com/51
- Is Null : https://rural-mouse.tistory.com/52
- Joins : https://rural-mouse.tistory.com/53
- Inner Join : https://rural-mouse.tistory.com/54
- Left Join : https://rural-mouse.tistory.com/55
- Group By : https://rural-mouse.tistory.com/56
- Having : https://rural-mouse.tistory.com/59
- Subqueries : https://rural-mouse.tistory.com/60
- Insert : https://rural-mouse.tistory.com/61
- Insert Multiple Rows : https://rural-mouse.tistory.com/62
- Insert Into Select ←←
- Update : https://rural-mouse.tistory.com/64
원문 : https://www.mariadbtutorial.com/mariadb-basics/mariadb-insert-into-select/
MariaDB Insert Into Select Statement By Practical Examples
In this tutorial, you will learn how to use the MariaDB insert into select statement to insert result sets of a query into a table.
www.mariadbtutorial.com
여기에 있는 모든 예제는 MariaDB sample database인 nation을 가지고 진행됩니다.
만약 읽고 따라 해 보시는 게 목적이라면 다운로드를 받고 읽어보시길 추천합니다.
원본 링크 : https://www.mariadbtutorial.com/getting-started/mariadb-sample-database/
MariaDB Sample Database
In this tutorial, you will learn about a MariaDB sample database nation and how to load the sample database into the MariaDB server.
www.mariadbtutorial.com
MariaDB의 Insert Into Select
Insert 명령문의 값들의 리스트는 리터럴 값들 혹은 쿼리 문의 Result set이 될 수도 있습니다.
다음은 insert 명령문에서 값 목록을 가져오는 Select 명령문의 구문을 보여줍니다.
insert into table_name(column_list)
select select_list
from table_name
...;
- 첫 번째로 데이터를 삽입하고 싶은 Column들의 리스트와 Table의 이름을 지정합니다.
- 두 번째로 column_list에 해당하는 Result_set을 반환하는 Select 명령문을 지정합니다.
insert into select 명령문은 하나의 Table에서 다른 Table로 데이터를 복제할 경우나 Table의 요약 데이터를 Table에 삽입할 때 매우 유용합니다.
MariaDB의 insert into select 문의 예제
샘플 데이터베이스의 countries와 region Table을 사용하도록 하겠습니다.
A) Table에서 다른 Table로 Row들을 삽입하는 예제
일단 small_countries라는 새로운 Table을 생성합니다.
create table small_countries(
country_id int primary key,
name varchar(50) not null,
area decimal(10,2) not null,
);
그 다음, small_countries Table에 countries의 50,000km2 보다 작은 면적을 가진 나라들을 삽입합니다.
insert into small_countries
(country_id,name,area)
select
country_id, name, area
from
countries
where
area < 50000;
세번째로 small_countries Table의 데이터를 확인합니다.
select *
from small_countries;
B) Table의 요약 데이터를 다른 Table에 삽입하는 예제
첫번째로 region_areas라고 하는 지역의 이름과 면적을 저장하는 새 Table을 만듭니다.
create table region_areas(
region_name varchar(100) not null,
region_area decimal(15,2) not null,
primary key(region_name)
);
두번째로 countries와 regions의 쿼리문을 만들고 region_areas에 삽입합니다.
insert into region_areas
(region_name, region_area)
select
regions.name,
sum(area)
from
countries
inner join regions
using (region_id)
group by
regions.name;
세번째로 region_areas Table의 데이터를 확인합니다.
select *
from
region_areas
order by
region_area;
이번 튜토리얼에서는 Insert 명령문의 삽입 데이터를 Select 명령문을 통해 가져오는 법을 배워봤습니다.
다음에는 데이터를 수정하는 update에 대해서 배워보겠습니다.
'번역 > MariaDB' 카테고리의 다른 글
[MariaDB Tutorial 번역] 19. Update (0) | 2021.07.14 |
---|---|
[MariaDB Tutorial 번역] 17. Insert Multiple Rows (bluk insert) (0) | 2021.07.12 |
[MariaDB Tutorial 번역] 16. Insert (0) | 2021.07.11 |
[MariaDB Tutorial 번역] 15. Subqueries (0) | 2021.07.09 |
[MariaDB Tutorial 번역] 14. Having (0) | 2021.07.08 |
댓글