gpt4 book ai didi

sql - 如何用另外两个表的数据填充一个新表?

转载 作者:行者123 更新时间:2023-12-04 03:05:16 24 4
gpt4 key购买 nike

我有一个需要修改的数据库。原始数据库由 3 个表组成。它现在需要再扩展一张表。

新表的数据来自原数据库的两个表。我可以创建表格,但无法用数据填充它。原始表格是:

create table Accommodatie
(plaatscode varchar(3) not null,
accommodatienr numeric(3) not null,
accommodatiename varchar(25) not null,
adres varchar(25) not null,
plaatsnaam varchar(20) not null,
land varchar(20) not null,
email varchar(30) null,
internet varchar(30) null,
contactpersoon varchar(25) null,
primary key (plaatscode, accommodatienr));

create table Vervoer
(vervoercode numeric(5) not null,
plaatscode varchar(3) not null,
stedenadviseur varchar(20) not null,
vervoersoort varchar(12) not null,
maatschappij varchar(15) not null,
omschrijving varchar(50) not null,
overstap varchar(30) null,
primary key (vervoercode));

create table Vervoerprijs
(vervoercode numeric(5) not null,
seizoen varchar(6) not null,
prijs numeric(6,2) not null,
plaatscode varchar(3) not null,
primary key (vervoercode, seizoen),
foreign key (vervoercode) references Vervoer(vervoercode));

新表如下所示:

CREATE TABLE Plaatscode
( plaatscode varchar(3) not null,
stedenadviseur varchar(20) not null,
land varchar(20) not null,
PRIMARY KEY (plaatscode));

对于新表,我需要 Accommodatie 中的 plaatscodeland 以及表 中的 vervoerscode vervoer.

你能帮我构建一个查询来填充新表吗?

最佳答案

如果您只想填充您的新表,那么以下方法应该可行:

INSERT INTO Plaatscode
(
plaatscode,
stedenadviseur,
land
)
SELECT
a.plaatscode, v.stedenadviseur, a.land
FROM Accommodatie a
INNER JOIN Vervoer
ON a.plaatscode = v.plaatscode

关于sql - 如何用另外两个表的数据填充一个新表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12555246/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com