从多个 sources/tables 插入
INSERT INTO from multiple sources/tables
我有一个 table FinalTable
包含这些列:
name, lastName, pesel, position, id_operator
我想用其他 2 个 table 的值填充我的 FinalTable
:
- AAA - 此 table 包含姓名、姓氏、姓氏、位置等列
- BBB - 这个 table 有列名称,id_operator,pesel
我想在 pesel
列加入 AAA
和 BBB
insert into FinalTable (name, lastName, pesel, position, id_operator)
select
name, lastName, pesel, position,
(select id_operator from BBB b where b.pesel = a.pesel)
from
AAA a;
怎么做?我想将最后一列 id_operator
设置为 BBB
中的值。上面的 SQL 查询不正确。
我要插入一个连接查询:
INSERT INTO FinalTable (name, lastName, pesel, position, id_operator)
SELECT a.name, a.lastName, a.pesel, a.position, b.id_operator
FROM AAA a
JOIN BBB b ON pesel = a.pesel;
在两个表之间使用join
insert into FinalTable (name, lastName, pesel, position, id_operator)
select name, lastName, pesel, position, id_operator
from AAA a join BBB b on b.pesel = a.pesel
insert into FinalTable (name, lastName, pesel, position, id_operator)
select name, lastName, pesel, position, id_operator from AAA a join BBB b on a.pesel=b.pesel;
您可以使用内部联接
insert into FinalTable (name, lastName, pesel, position, id_operator)
select a.name, a.lastName, a.pesel, a.position, b.id_operator
from AAA a
INNER JOIN BBB b ON b.pesel = a.pesel
;
我有一个 table FinalTable
包含这些列:
name, lastName, pesel, position, id_operator
我想用其他 2 个 table 的值填充我的 FinalTable
:
- AAA - 此 table 包含姓名、姓氏、姓氏、位置等列
- BBB - 这个 table 有列名称,id_operator,pesel
我想在 pesel
列加入 AAA
和 BBB
insert into FinalTable (name, lastName, pesel, position, id_operator)
select
name, lastName, pesel, position,
(select id_operator from BBB b where b.pesel = a.pesel)
from
AAA a;
怎么做?我想将最后一列 id_operator
设置为 BBB
中的值。上面的 SQL 查询不正确。
我要插入一个连接查询:
INSERT INTO FinalTable (name, lastName, pesel, position, id_operator)
SELECT a.name, a.lastName, a.pesel, a.position, b.id_operator
FROM AAA a
JOIN BBB b ON pesel = a.pesel;
在两个表之间使用join
insert into FinalTable (name, lastName, pesel, position, id_operator)
select name, lastName, pesel, position, id_operator
from AAA a join BBB b on b.pesel = a.pesel
insert into FinalTable (name, lastName, pesel, position, id_operator)
select name, lastName, pesel, position, id_operator from AAA a join BBB b on a.pesel=b.pesel;
您可以使用内部联接
insert into FinalTable (name, lastName, pesel, position, id_operator)
select a.name, a.lastName, a.pesel, a.position, b.id_operator
from AAA a
INNER JOIN BBB b ON b.pesel = a.pesel
;