SQL 查看连接 3 个表,但有多个列用于 ON 子句
SQL view to join 3 tables, but there are multiple columns to use for the ON clause
我们在 PostgreSQL 中有一个数据库用于电池信息。
- 材料(原料化学品materials)
- 由原始 materials
制成的电极(阴极或阳极)
- 由电极和电解质构成的电池(materials)
每个 table 中都有一个唯一的 "Key"(第一列)来标识该项目。所以电极将指向 materials。问题是阴极和阳极列 Cells table 指的是电极 table(以及粘合剂、活性 materials、溶剂,在电极中指向 materials)。
我目前的观点是这样的。但我知道这是不正确的:
CREATE OR REPLACE VIEW se3d.m2elect2cell AS
SELECT
m."Key" as "Mat Key",
m."Chemical",
m."Description" as "Material Description",
e."Description" as "Electrode Description",
e."Role" as "Electrode Role",
c."Key" as "Cell Key",
c."Description" as "Cell Description"
FROM se3d.materials m
INNER JOIN se3d.electrodes e ON (e."Active Material" = m."Key")
INNER JOIN se3d.cells c ON (c."Cathode" = e."Key")
我们正在寻找的查询类型是查找具有特定 material 属性 的所有电池,或者从电池中查找其制造中使用的 material。
相关 table 的定义如下所示:
CREATE TABLE se3d.materials
(
"Key" character varying COLLATE pg_catalog."default" NOT NULL,
"Chemical" character varying(255) COLLATE pg_catalog."default",
"Description" character varying(255) COLLATE pg_catalog."default",
"Created" date,
"Created By" character varying(255) COLLATE pg_catalog."default",
"Modified" date,
"Modified By" character varying(255) COLLATE pg_catalog."default",
"Version" double precision,
"Chemical Type" character varying(255) COLLATE pg_catalog."default",
"Storage" character varying(255) COLLATE pg_catalog."default",
"Tags" character varying(255) COLLATE pg_catalog."default",
CONSTRAINT materials_pkey PRIMARY KEY ("Key")
)
CREATE TABLE se3d.electrodes
(
"Key" character varying COLLATE pg_catalog."default" NOT NULL,
"Active Material" character varying(255) COLLATE pg_catalog."default",
"Active Material mass (g)" double precision,
"Binder" character varying(255) COLLATE pg_catalog."default",
"Binder mass (g)" double precision,
"Conductive Additive" character varying(255) COLLATE pg_catalog."default",
"Carbon mass (g)" double precision,
"Created" date,
"Created By" character varying(255) COLLATE pg_catalog."default",
"Creator" character varying(255) COLLATE pg_catalog."default",
"Description" character varying(255) COLLATE pg_catalog."default",
"Modified" date,
"Modified By" character varying(255) COLLATE pg_catalog."default",
"Role" character varying(255) COLLATE pg_catalog."default",
"Solvent" character varying(255) COLLATE pg_catalog."default",
"Solvent mass (g)" double precision,
"Version" double precision,
"Creation Date" date,
"Tags" character varying(255) COLLATE pg_catalog."default",
CONSTRAINT electrodes_pkey PRIMARY KEY ("Key")
)
CREATE TABLE se3d.cells
(
"Key" character varying COLLATE pg_catalog."default" NOT NULL,
"Creator" character varying(255) COLLATE pg_catalog."default",
"Creation Date" date,
"Electrolyte" character varying(255) COLLATE pg_catalog."default",
"Anode" character varying(255) COLLATE pg_catalog."default",
"Cathode" character varying(255) COLLATE pg_catalog."default",
"Separator" character varying(255) COLLATE pg_catalog."default",
"Form Factor" character varying(255) COLLATE pg_catalog."default",
"Test Plan" character varying(255) COLLATE pg_catalog."default",
"Description" character varying(255) COLLATE pg_catalog."default",
"Created" date,
"Created By" character varying(255) COLLATE pg_catalog."default",
"Modified" date,
"Modified By" character varying(255) COLLATE pg_catalog."default",
"Version" double precision,
"Tags" character varying(255) COLLATE pg_catalog."default",
CONSTRAINT cells_pkey PRIMARY KEY ("Key")
)
如果您将电极角色扮演为阳极和阴极...
SELECT
m."Key" as "Mat Key",
m."Chemical",
m."Description" as "Material Description",
anode."Description" as "Anode Description",
anode."Role" as "Anode Role",
cathode."Description" as "Cathode Description",
cathode."Role" as "Cathode Role",
c."Key" as "Cell Key",
c."Description" as "Cell Description"
FROM se3d.materials m
INNER JOIN se3d.electrodes anode ON (anode."Active Material" = m."Key")
INNER JOIN se3d.electrodes cathode ON (cathode."Active Material" = m."Key")
INNER JOIN se3d.cells c ON (c."Cathode" = cathode."Key")
or (c."Anode" = anode."Key")
The kind of queries we are looking for is finding all cells with a particular material property, or going from cell to find the materials used in its manufacture.
您可能找不到一种视图可以有效地满足这两种用途。您可能需要多个视图,或者直接编写您想要的查询,然后 运行 它们没有视图。
我们在 PostgreSQL 中有一个数据库用于电池信息。
- 材料(原料化学品materials)
- 由原始 materials 制成的电极(阴极或阳极)
- 由电极和电解质构成的电池(materials)
每个 table 中都有一个唯一的 "Key"(第一列)来标识该项目。所以电极将指向 materials。问题是阴极和阳极列 Cells table 指的是电极 table(以及粘合剂、活性 materials、溶剂,在电极中指向 materials)。
我目前的观点是这样的。但我知道这是不正确的:
CREATE OR REPLACE VIEW se3d.m2elect2cell AS
SELECT
m."Key" as "Mat Key",
m."Chemical",
m."Description" as "Material Description",
e."Description" as "Electrode Description",
e."Role" as "Electrode Role",
c."Key" as "Cell Key",
c."Description" as "Cell Description"
FROM se3d.materials m
INNER JOIN se3d.electrodes e ON (e."Active Material" = m."Key")
INNER JOIN se3d.cells c ON (c."Cathode" = e."Key")
我们正在寻找的查询类型是查找具有特定 material 属性 的所有电池,或者从电池中查找其制造中使用的 material。
相关 table 的定义如下所示:
CREATE TABLE se3d.materials
(
"Key" character varying COLLATE pg_catalog."default" NOT NULL,
"Chemical" character varying(255) COLLATE pg_catalog."default",
"Description" character varying(255) COLLATE pg_catalog."default",
"Created" date,
"Created By" character varying(255) COLLATE pg_catalog."default",
"Modified" date,
"Modified By" character varying(255) COLLATE pg_catalog."default",
"Version" double precision,
"Chemical Type" character varying(255) COLLATE pg_catalog."default",
"Storage" character varying(255) COLLATE pg_catalog."default",
"Tags" character varying(255) COLLATE pg_catalog."default",
CONSTRAINT materials_pkey PRIMARY KEY ("Key")
)
CREATE TABLE se3d.electrodes
(
"Key" character varying COLLATE pg_catalog."default" NOT NULL,
"Active Material" character varying(255) COLLATE pg_catalog."default",
"Active Material mass (g)" double precision,
"Binder" character varying(255) COLLATE pg_catalog."default",
"Binder mass (g)" double precision,
"Conductive Additive" character varying(255) COLLATE pg_catalog."default",
"Carbon mass (g)" double precision,
"Created" date,
"Created By" character varying(255) COLLATE pg_catalog."default",
"Creator" character varying(255) COLLATE pg_catalog."default",
"Description" character varying(255) COLLATE pg_catalog."default",
"Modified" date,
"Modified By" character varying(255) COLLATE pg_catalog."default",
"Role" character varying(255) COLLATE pg_catalog."default",
"Solvent" character varying(255) COLLATE pg_catalog."default",
"Solvent mass (g)" double precision,
"Version" double precision,
"Creation Date" date,
"Tags" character varying(255) COLLATE pg_catalog."default",
CONSTRAINT electrodes_pkey PRIMARY KEY ("Key")
)
CREATE TABLE se3d.cells
(
"Key" character varying COLLATE pg_catalog."default" NOT NULL,
"Creator" character varying(255) COLLATE pg_catalog."default",
"Creation Date" date,
"Electrolyte" character varying(255) COLLATE pg_catalog."default",
"Anode" character varying(255) COLLATE pg_catalog."default",
"Cathode" character varying(255) COLLATE pg_catalog."default",
"Separator" character varying(255) COLLATE pg_catalog."default",
"Form Factor" character varying(255) COLLATE pg_catalog."default",
"Test Plan" character varying(255) COLLATE pg_catalog."default",
"Description" character varying(255) COLLATE pg_catalog."default",
"Created" date,
"Created By" character varying(255) COLLATE pg_catalog."default",
"Modified" date,
"Modified By" character varying(255) COLLATE pg_catalog."default",
"Version" double precision,
"Tags" character varying(255) COLLATE pg_catalog."default",
CONSTRAINT cells_pkey PRIMARY KEY ("Key")
)
如果您将电极角色扮演为阳极和阴极...
SELECT
m."Key" as "Mat Key",
m."Chemical",
m."Description" as "Material Description",
anode."Description" as "Anode Description",
anode."Role" as "Anode Role",
cathode."Description" as "Cathode Description",
cathode."Role" as "Cathode Role",
c."Key" as "Cell Key",
c."Description" as "Cell Description"
FROM se3d.materials m
INNER JOIN se3d.electrodes anode ON (anode."Active Material" = m."Key")
INNER JOIN se3d.electrodes cathode ON (cathode."Active Material" = m."Key")
INNER JOIN se3d.cells c ON (c."Cathode" = cathode."Key")
or (c."Anode" = anode."Key")
The kind of queries we are looking for is finding all cells with a particular material property, or going from cell to find the materials used in its manufacture.
您可能找不到一种视图可以有效地满足这两种用途。您可能需要多个视图,或者直接编写您想要的查询,然后 运行 它们没有视图。