复合主键上的 SQLITE JOIN 表

SQLITE JOIN tables on composite primary key

在 TCL 脚本中使用 SQLlite 我有三个 tables:

            DB eval { CREATE TABLE CompFIs       ( EID int NOT NULL , PlyID  int NOT NULL  , LC int NOT NULL , FI float NOT NULL , SR float   , ImagePath text ,   PRIMARY KEY ( LC, EID, PlyID )) }
            DB eval { CREATE TABLE CompStresses  ( EID int NOT NULL , PlyID  int NOT NULL  , LC int NOT NULL , sigx float        , sigy float , sigxy float    ,   PRIMARY KEY ( LC, EID, PlyID )) }
            DB eval { CREATE TABLE ElemMetadata  ( EID int NOT NULL , AssyID int NOT NULL ,  PID int  ,  PRIMARY KEY ( EID )) }

我现在想知道给定 AssID 从第一个 table 'CompFIs' 开始的最大 FI。 对于那个 FI(故障指数),我需要来自其他 table 的相应值。 为了实现这一点,我想我使用主键加入 tables。

但是我没有找到如何使用他们的 PK 加入 tables。 相反,我尝试了以下方法:

SELECT
ElemMetadata.EID     AS EID      ,
ElemMetadata.AssyID  AS AssyID   ,
ElemMetadata.PID     AS PID      ,
CompFIs.PlyID        AS Ply      ,
CompFIs.LC           AS LC       ,
CompFIs.FI           AS FI       ,
CompFIs.SR           AS SR       ,
CompStresses.sigx    AS sigx     ,
CompStresses.sigy    AS sigy     ,
CompStresses.sigxy   AS sigxy
FROM
CompFIs 
LEFT JOIN CompStresses ON CompFIs.EID = CompStresses.EID
LEFT JOIN ElemMetadata ON CompFIs.EID = ElemMetadata.EID
WHERE AssyID = 1 ORDER BY  FI DESC LIMIT 5

这不起作用,因为数据仅使用 EID 列匹配。

如何根据 table 的复合 PK 加入?

示例值:

Table: CompFIs
--------------------------------------------------
EID     PlyID   LC      FI      SR      ImagePath 
--------------------------------------------------
282812  7       1       1.2824  null    null 
282813  2       3       0.3333  null    null

Table: ElemMetadata
--------------------------------------------------
EID     AssyID  PID
--------------------------------------------------
282812  1       51
282813  1       51

Table: CompStresses
--------------------------------------------------
EID     PlyID   LC      sigx    sigy    sigxy
--------------------------------------------------
282812  1       1       30.7    169.9   -31.13
282813  1       1       12.7    180.7   55.2


Expected output:
-------------------------------------------------------------------------------
EID     AssyID  PID     Ply     LC      FI      SR      sigx    sigy    sigxy
-------------------------------------------------------------------------------
282812  1       51      1       1       1.2824  null    30.7    169.9   -31.13

感谢任何帮助。

使用 CTENOT EXISTS:

with cte as (
  SELECT
  ElemMetadata.EID     AS EID      ,
  ElemMetadata.AssyID  AS AssyID   ,
  ElemMetadata.PID     AS PID      ,
  CompFIs.PlyID        AS Ply      ,
  CompFIs.LC           AS LC       ,
  CompFIs.FI           AS FI       ,
  CompFIs.SR           AS SR       ,
  CompStresses.sigx    AS sigx     ,
  CompStresses.sigy    AS sigy     ,
  CompStresses.sigxy   AS sigxy
  FROM
  CompFIs 
  LEFT JOIN CompStresses ON CompFIs.EID = CompStresses.EID
  LEFT JOIN ElemMetadata ON CompFIs.EID = ElemMetadata.EID 
  WHERE AssyID = 1 
  ORDER BY  FI DESC LIMIT 5
) 

select c.* from cte c 
where not exists (
  select 1 from cte
  where FI > c.FI
)

参见demo
结果:

| EID    | AssyID | PID | Ply | LC  | FI     | SR  | sigx | sigy  | sigxy  |
| ------ | ------ | --- | --- | --- | ------ | --- | ---- | ----- | ------ |
| 282812 | 1      | 51  | 7   | 1   | 1.2824 |     | 30.7 | 169.9 | -31.13 |