我有一个带有这些列的表FinalTable
:
name, lastName, pesel, position, id_operator
我想用另外2个表中的值填充我的FinalTable
:
我想加入AAA
列上的BBB
和pesel
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查询不正确。
答案 0 :(得分:2)
我要插入一个联接查询:
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;
答案 1 :(得分:2)
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;
答案 2 :(得分:1)
在两个表之间使用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
答案 3 :(得分:1)
您可以使用内部联接
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
;