2016-12-07 6 views
0

私は2つのテーブル(ポイントとtxt)を持っています。 テーブルポイントでは、IDはテーブルtxtのテキストへの参照とともに保存されます。SQLクエリ - 複数のフィールドを1つのレコードに結合する

データベースをクエリし、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

のようになります。参加残さやっテキストテーブルマッチごとにポイントデータの行を複製したくない場合は、使用可能なマッチから1つのレコードを選択するための基準が必要です。典型的な値は、最大値または最新の日付に基づいています。 –

答えて

0

私の明確化を確認するために、あなたの "ポイント"テーブルのような音は、それに多くのコンポーネントを持つアイテムのように動作します。各構成要素は、IDおよび対応するテキストを有するルックアップ・テーブル「txt」を指し示す。したがって、1つの「ポイント」レコードには、この場合は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 
関連する問題