1

4 つの SQL Server データベース テーブルがあります。

  1. inventory
  2. inventory2
  3. bdo
  4. details

構造は次のとおりです。

在庫

AllocationID   MSISDN
1              3018440225
2              3028431115

在庫2

    AllocationID   MSISDN
    1              3011234567
    2              3026440657
    3              3454159650

BDO

BDO_ID      BDO_MSISDN
1           3457076952
2           3005000475

詳細

AllocationID    MSISDN
3               3454159650

次に、次のクエリからレコードを取得する必要があります。

select a.msisdn, b.bdo_id
from details a, bdo b, inventory c, inventory2 d
where 
    a.msisdn = 3454159650
    and (a.allocationid = c.allocationid) or (a.allocationid = d.allocationid)
    and (c.bdo_id = b.bdo_id) or (d.bdo_id = b.bdo_id)

このクエリは複数の結果を返します (すべてまったく同じです) なぜそうなのですか? 私が間違っている場合は、私の概念とクエリを修正してください。

4

3 に答える 3

2

クエリの非常に奇妙なフォームがあります。まず、join構文を使用する必要があります。2 つ目は、2 つの在庫テーブルの結合が必要なようです。

select d.msisdn, b.bdo_id
from (select i.*
      from (select i.* from inventory i union all
            select i2.* from inventory i2
           ) i
     ) i join
     details d
     on d.allocationid = i.allocationid join
     bdo b
     on i.bdo_id=b.bdo_id
where d.msisdn = 3454159650;

クエリを明示的な結合として構造化すると、クエリがより効率的になり、理解、修正、保守が容易になります。

編集:

一部のテーブルで一部のレコードが欠落している可能性があります。でこのバージョンを使用してみてくださいleft outer join:

select d.msisdn, b.bdo_id
from details d left outer join
     (select i.*
      from (select i.* from inventory i union all
            select i2.* from inventory i2
           ) i
     ) i
     details d
     on d.allocationid = i.allocationid left outer join
     bdo b
     on i.bdo_id=b.bdo_id
where d.msisdn = 3454159650;
于 2013-08-24T11:23:17.013 に答える