0

人々

以下に説明するように、2つのテーブルcountrycodes、cctonumbersがあります。

試したクエリがありますが、目的の出力が得られません。

私の出力

 country          destination                     country_code   destination_code

Afghanistan  Afghanistan Mobile Etisalat              93        78
Afghanistan  Afghanistan Mobile Etisalat              93        72
Afghanistan  Afghanistan Mobile Roshan                93        79
Afghanistan  Afghanistan                              93        93

望ましい出力

country          destination                     country_code   destination_code

Afghanistan  Afghanistan Mobile Etisalat             93           78

Afghanistan  Afghanistan Mobile Etisalat             93           72    

Afghanistan  Afghanistan Mobile Roshan               93           79

使用したテーブルは以下の通り

国番号表

id       parentid   countryname 
1031     0          afghanistan 
1035     1031       Afghanistan Mobile Etisalat
1036     1031       Afghanistan Mobile Roshan

cctonumbers テーブル

id       countrycode_id     parentid         number 
15731    1031               0                93
15197    1035               15731            78
15198    1035               15731            72
15199    1036               15731            79

私が使用しているクエリは以下のとおりですが、望ましい結果が得られません。

select * 
   from 
      cctonumbers 
         LEFT JOIN countrycodes as CC 
            ON cctonumbers.countrycode_id = CC.id 
   WHERE 
      (     CC.parentid=0 
        AND number like '93%'
        and cctonumbers.id in 
                ( select cctonumbers.parentid 
                     from cctonumbers 
                        LEFT JOIN countrycodes as CC 
                           ON cctonumbers.countrycode_id = CC.id 
                        WHERE number like '7%'
                          AND CC.parentid!=0 )
                ) 
          or (     CC.parentid != 0 
               AND number like '7%' 
               AND CC.parentid in 
                       ( select CC.id 
                            from cctonumbers 
                               LEFT JOIN countrycodes as CC 
                                  ON cctonumbers.countrycode_id=CC.id 
                            WHERE CC.parentid=0 
                              AND number like '93%' )
             ) 
   ORDER BY 
      cctonumbers.number Asc
4

1 に答える 1

0

あなたが探しているのは、特定の国コードに関連付けられたすべての目的地です。実際には cctonumbers.id = 15731 の結果が表示されていないため、結果を取得するために回転していたと思います。木を下るのではなく逆さまにしましょう。

親 ID を持つ CCToNumbers レコードだけから始めます。1 レベルの深さしかないように見えます。これらの子レベルのエントリを取得したら、その親の CCToNumbers に再び参加します...次に、両方の国コード テーブルにそれぞれ移動します。

SELECT
      CtoN.ID,
      PCountry.CountryName,
      DCountry.CountryName as Destination,
      CtoNParent.Number as Country_Code,
      CtoN.Number as Destination_Code
   from 
      cctonumbers CtoN
         JOIN cctonumbers as CtoNParent
             ON CtoN.parentid = CtoNParent.ID
            AND CtoNParent.number like '93%'
            JOIN countrycodes as PCountry
               ON CtoNParent.CountryCode_Id = PCountry.ID
         JOIN countrycodes as DCountry
            ON CtoN.CountryCode_Id = DCountry.ID
   where
      CtoN.number like '7%'
于 2013-10-02T13:49:07.940 に答える