您可以使用联合创建单个查询
SELECT Date, Cars, Boats, Tags, Guns,
[Missing Persons], Null AS [Bench Warrants], Null AS Fingerprints FROM UnitA
UNION ALL
SELECT Date, Cars, Boats, Tags, Guns,
Null, [Bench Warrants], Null FROM UnitB
UNION ALL
SELECTG Date, Cars, Boats, Tags, Guns,
Null, Null, Fingerprints FROM UnitC
每个 SELECT 中的字段数必须相同。第一个 SELECT 的字段名称将用于结果。
但是,我建议另一种数据库设计。将所有常用字段放在一个公共表中,并将特定于单位的字段放在您以 1 对 1 关系连接的其他表中
CREATE TABLE Unit (
UnitID CONSTRAINT PK_unit PRIMARY KEY,
Date datetime,
Cars long,
Boats long,
Tags long,
Guns long
)
CREATE TABLE UnitA (
UnitID CONSTRAINT PK_unitA PRIMARY KEY,
[Missing Persons] Long
)
ALTER TABLE UnitA
ADD CONSTRAINT FK_unitA_unit
FOREIGN KEY (UnitID) REFERENCES Unit (UnitID)
CREATE TABLE UnitB (
UnitID CONSTRAINT PK_unitB PRIMARY KEY,
[Bench Warrants] Long
)
ALTER TABLE UnitB
ADD CONSTRAINT FK_unitB_unit
FOREIGN KEY (UnitID) REFERENCES Unit (UnitID)
CREATE TABLE UnitC (
UnitID CONSTRAINT PK_unitC PRIMARY KEY,
Fingerprints Long
)
ALTER TABLE UnitC
ADD CONSTRAINT FK_unitC_unit
FOREIGN KEY (UnitID) REFERENCES Unit (UnitID)
现在您可以使用左连接创建查询。
一个更简单的设计是只创建一个表,并允许空字段
CREATE TABLE Unit (
UnitID CONSTRAINT PK_unit PRIMARY KEY,
Date datetime,
Cars long,
Boats long,
Tags long,
Guns long,
[Missing Persons] Long,
[Bench Warrants] Long,
Fingerprints Long
)
无需直接导入 excel 表,只需链接 excel 表,然后使用自定义追加查询将其内容附加到公用表。