复合主键上的SQLITE JOIN表

时间:2019-07-29 12:18:40

标签: sqlite join composite-primary-key

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

            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的第一个表“ CompFIs”中了解最大FI。 对于那个FI(故障指数),我需要其他表中的相应值。 为了达到这个目的,我使用主键加入表。

但是,我找不到如何使用其PK联接表的方法。 相反,我尝试了以下操作:

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列匹配数据。

如何基于表的复合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

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

使用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 |