MariaDB 10.6にINSERT SELECT文で別のテーブルのデータをテーブルに追加する

環境
Windows10 64bit
MariaDB 10.6.4

書式
INSERT INTO テーブルA (カラム1,カラム2,…)
SELECT カラム1,カラム2,… FROM テーブルB
SELECT文で別のテーブルからデータを取得し、INSERT文でテーブルにデータを追加します。
別のテーブルに格納されているデータを取得してテーブルに追加します。

使用例
1.テーブルを作成します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
MariaDB [testdb]> create table testA (tid int auto_increment, tname varchar(120), age int, index(tid));
Query OK, 0 rows affected (0.272 sec)
MariaDB [testdb]> create table testA (tid int auto_increment, tname varchar(120), age int, index(tid)); Query OK, 0 rows affected (0.272 sec)
MariaDB [testdb]> create table testA (tid int auto_increment, tname varchar(120), age int, index(tid));
Query OK, 0 rows affected (0.272 sec)

2.データをテーブルに追加します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
MariaDB [testdb]> insert into testA (tname, age) values('中村', 13);
Query OK, 1 row affected (0.125 sec)
MariaDB [testdb]> insert into testA (tname, age) values('村上', 23);
Query OK, 1 row affected (0.036 sec)
MariaDB [testdb]> insert into testA (tname, age) values('上村', 33);
Query OK, 1 row affected (0.066 sec)
MariaDB [testdb]> insert into testA (tname, age) values('中村', 13); Query OK, 1 row affected (0.125 sec) MariaDB [testdb]> insert into testA (tname, age) values('村上', 23); Query OK, 1 row affected (0.036 sec) MariaDB [testdb]> insert into testA (tname, age) values('上村', 33); Query OK, 1 row affected (0.066 sec)
MariaDB [testdb]> insert into testA (tname, age) values('中村', 13);
Query OK, 1 row affected (0.125 sec)
MariaDB [testdb]> insert into testA (tname, age) values('村上', 23);
Query OK, 1 row affected (0.036 sec)
MariaDB [testdb]> insert into testA (tname, age) values('上村', 33);
Query OK, 1 row affected (0.066 sec)

3.「testA」テーブルにデータが格納されることを確認します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
MariaDB [testdb]> select * from testA;
+-----+-------+------+
| tid | tname | age |
+-----+-------+------+
| 1 | 中村 | 13 |
| 2 | 村上 | 23 |
| 3 | 上村 | 33 |
+-----+-------+------+
3 rows in set (0.000 sec)
MariaDB [testdb]> select * from testA; +-----+-------+------+ | tid | tname | age | +-----+-------+------+ | 1 | 中村 | 13 | | 2 | 村上 | 23 | | 3 | 上村 | 33 | +-----+-------+------+ 3 rows in set (0.000 sec)
MariaDB [testdb]> select * from testA;
+-----+-------+------+
| tid | tname | age  |
+-----+-------+------+
|   1 | 中村  |   13 |
|   2 | 村上  |   23 |
|   3 | 上村  |   33 |
+-----+-------+------+
3 rows in set (0.000 sec)

4.「testB」テーブルを作成します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
MariaDB [testdb]> create table testB (tid int, tname varchar(20), taddr varchar(30), tage int);
Query OK, 0 rows affected (0.259 sec)
MariaDB [testdb]> create table testB (tid int, tname varchar(20), taddr varchar(30), tage int); Query OK, 0 rows affected (0.259 sec)
MariaDB [testdb]> create table testB (tid int, tname varchar(20), taddr varchar(30), tage int);
Query OK, 0 rows affected (0.259 sec)

5.データを「testB」テーブルに追加します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
MariaDB [testdb]> insert into testB values(1, '山田', '横浜', 15);
Query OK, 1 row affected (0.032 sec)
MariaDB [testdb]> insert into testB values(2, '澤田', '東京', 25);
Query OK, 1 row affected (0.063 sec)
MariaDB [testdb]> insert into testB values(3, '山形', '名古屋', 35);
Query OK, 1 row affected (0.069 sec)
MariaDB [testdb]> insert into testB values(1, '山田', '横浜', 15); Query OK, 1 row affected (0.032 sec) MariaDB [testdb]> insert into testB values(2, '澤田', '東京', 25); Query OK, 1 row affected (0.063 sec) MariaDB [testdb]> insert into testB values(3, '山形', '名古屋', 35); Query OK, 1 row affected (0.069 sec)
MariaDB [testdb]> insert into testB values(1, '山田', '横浜', 15);
Query OK, 1 row affected (0.032 sec)
MariaDB [testdb]> insert into testB values(2, '澤田', '東京', 25);
Query OK, 1 row affected (0.063 sec)
MariaDB [testdb]> insert into testB values(3, '山形', '名古屋', 35);
Query OK, 1 row affected (0.069 sec)

データ一覧を確認します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
MariaDB [testdb]> select * from testB;
+------+-------+--------+------+
| tid | tname | taddr | tage |
+------+-------+--------+------+
| 1 | 山田 | 横浜 | 15 |
| 2 | 澤田 | 東京 | 25 |
| 3 | 山形 | 名古屋 | 35 |
+------+-------+--------+------+
3 rows in set (0.000 sec)
MariaDB [testdb]> select * from testB; +------+-------+--------+------+ | tid | tname | taddr | tage | +------+-------+--------+------+ | 1 | 山田 | 横浜 | 15 | | 2 | 澤田 | 東京 | 25 | | 3 | 山形 | 名古屋 | 35 | +------+-------+--------+------+ 3 rows in set (0.000 sec)
MariaDB [testdb]> select * from testB;
+------+-------+--------+------+
| tid  | tname | taddr  | tage |
+------+-------+--------+------+
|    1 | 山田  | 横浜   |   15 |
|    2 | 澤田  | 東京   |   25 |
|    3 | 山形  | 名古屋 |   35 |
+------+-------+--------+------+
3 rows in set (0.000 sec)

6.「testB」に含まれるデータを取得しtestAテーブルに追加します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
MariaDB [testdb]> insert into testA (tname, age) select tname, tage from testB;
Query OK, 3 rows affected (0.214 sec)
Records: 3 Duplicates: 0 Warnings: 0
MariaDB [testdb]> insert into testA (tname, age) select tname, tage from testB; Query OK, 3 rows affected (0.214 sec) Records: 3 Duplicates: 0 Warnings: 0
MariaDB [testdb]> insert into testA (tname, age) select tname, tage from testB;
Query OK, 3 rows affected (0.214 sec)
Records: 3  Duplicates: 0  Warnings: 0

結果を確認します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
MariaDB [testdb]> select * from testA;
+-----+-------+------+
| tid | tname | age |
+-----+-------+------+
| 1 | 中村 | 13 |
| 2 | 村上 | 23 |
| 3 | 上村 | 33 |
| 4 | 山田 | 15 |
| 5 | 澤田 | 25 |
| 6 | 山形 | 35 |
+-----+-------+------+
6 rows in set (0.000 sec)
MariaDB [testdb]> select * from testA; +-----+-------+------+ | tid | tname | age | +-----+-------+------+ | 1 | 中村 | 13 | | 2 | 村上 | 23 | | 3 | 上村 | 33 | | 4 | 山田 | 15 | | 5 | 澤田 | 25 | | 6 | 山形 | 35 | +-----+-------+------+ 6 rows in set (0.000 sec)
MariaDB [testdb]> select * from testA;
+-----+-------+------+
| tid | tname | age  |
+-----+-------+------+
|   1 | 中村  |   13 |
|   2 | 村上  |   23 |
|   3 | 上村  |   33 |
|   4 | 山田  |   15 |
|   5 | 澤田  |   25 |
|   6 | 山形  |   35 |
+-----+-------+------+
6 rows in set (0.000 sec)

 

MariaDB

Posted by arkgame