SQL查询 - 将多个字段连接到一个记录

时间:2016-12-07 12:58:09

标签: sql join

我有两个表(point和txt)。 在表格点中,保存id,并引用表txt中的文本。

我想查询数据库并从点表中获取一条记录,其中id被替换为表txt中的文本。

我现在有这个查询,但这会为表格点中的每条记录创建9条记录。

SELECT
   point.useradr, point.descr, point.plant, point.turnoff, point.position,
   point.instruction, point.restart, point.workcall, point.workinform, 
   point.offcall, point.offinform, point.procedur,
   txt.id, txt.txt
FROM point 
JOIN txt ON
   point.useradr = 'G2.F.22.CTS.CU0.90' AND
   ( point.plant = txt.id OR
     point.turnoff = txt.id OR
     point.position = txt.id OR
     point.instruction = txt.id OR
     point.restart = txt.id OR
     point.workcall = txt.id OR
     point.workinform = txt.id OR
     point.offcall = txt.id OR
     point.offinform = txt.id OR
     point.procedur = txt.id )
 ORDER BY txt.id

1 个答案:

答案 0 :(得分:0)

为了确认我的澄清,它的声音就像你的“点”表就像一个有很多组件的项目。每个组件指向具有ID和相应文本的查找表“txt”。因此,一个“点”记录将显示10个单词。

您可以使用多个左连接并应用别名或单个连接,但可以按照未明确列出的每个单点ID记录应用聚合组。像下面的小组一样

SELECT
      P.id,
      MAX( P.useradr ) UserAdr, 
      MAX( P.descr ) Descr, 
      MAX( case when T.ID = P.plant then T.txt end ) as PlantDescrip, 
      MAX( case when T.ID = P.turnoff then T.txt end ) as TurnOffDescrip, 
      MAX( case when T.ID = P.position then T.txt end ) as PositionDescrip, 
      MAX( case when T.ID = P.instruction then T.txt end ) as InstructionDescrip, 
      MAX( case when T.ID = P.restart then T.txt end ) as RestartDescrip, 
      MAX( case when T.ID = P.workcall then T.txt end ) as WorkCallDescrip,
      MAX( case when T.ID = P.workinform then T.txt end ) as WorkInFormDescrip, 
      MAX( case when T.ID = P.offcall then T.txt end ) as OffCallDescrip, 
      MAX( case when T.ID = P.offinform then T.txt end ) as OffInFormDescrip, 
      MAX( case when T.ID = P.procedur then T.txt end ) as ProcedurDescrip
   from 
         point P
            join txt T
               ON T.id IN ( P.plant, P.turnoff, P.position,
                     P.instruction, P.restart, P.workcall,
                     P.workinform, P.offcall, P.offinform, P.procedur)
   where
      P.useradr = 'G2.F.22.CTS.CU0.90' 
   group by
      P.id
   order by 
      P.id

通过使用多个连接,因为Point“text Id”列链接将存在或不存在, 做左连接看起来像

SELECT
      P.id,
      P.useradr, 
      P.descr, 
      ByPlant.txt as PlantDescrip, 
      ByTurnOff.txt as TurnOffDescrip, 
      ByPosition.txt as PositionDescrip, 
      ByInstruction.txt as InstructionDescrip, 
      ByRestart.txt as RestartDescrip, 
      ByWorkcall.txt as WorkCallDescrip,
      ByWorkinform.txt as WorkInFormDescrip, 
      ByOffcall.txt as OffCallDescrip, 
      ByOffinform.txt as OffInFormDescrip, 
      ByProcedur.txt as ProcedurDescrip
   from 
         point P
            Left JOIN txt ByPlant
                ON P.Plant = ByPlant.ID
            Left JOIN txt ByTurnOff
                ON P.turnoff = ByTurnOff.ID
            Left JOIN txt ByPosition
                ON P.position = ByPosition.ID
            Left JOIN txt ByInstruction
                ON P.instruction = ByInstruction.ID
            Left JOIN txt ByRestart
                ON P.restart = ByRestart.ID
            Left JOIN txt ByWorkcall
                ON P.Workcall = ByWorkcall.ID
            Left JOIN txt ByWorkinform
                ON P.Workinform = ByWorkinform.ID
            Left JOIN txt ByOffcall
                ON P.Offcall = ByOffcall.ID
            Left JOIN txt ByOffinform
                ON P.Offinform= ByOffinform.ID
            Left JOIN txt ByProcedur
                ON P.Procedur = ByProcedur.ID
   where
      P.useradr = 'G2.F.22.CTS.CU0.90' 
   order by 
      P.id