2016-12-07 65 views
0

我有两个表格(point和txt)。 在表格中,一个id与表格txt中的一个文本的引用一起保存。SQL Query - 将多个字段加入一条记录

我想查询数据库并从点表中获取记录,其中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 
+0

你越来越多场比赛,文本表。如果不想为每场比赛复制点数据行,则需要一些条件从可用匹配中选择一条记录。典型值取决于最大值或最近的日期。 –

回答

0

为了确认我的说明,它听起来像你的“点”表行为像一个项目,它有很多组件。每个组件都指向一个查找表“txt”,它具有ID和相应的文本。因此,在这种情况下,一个“点”记录将显示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 

通过使用多个连接“文章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 
相关问题