0

親会社が存在する場合はどちらかを選択する必要があり、親会社が存在せず、2 番目の会社が存在する場合はそれを選択する必要があり、両方が存在しない場合は会社 (子会社) を選択する必要があるクエリを作成しています。 )。

ID と会社名を持つ Company テーブルがあるとします。

次の列を持つ Movie テーブルもあります。

  • ID (映画 ID 番号を参照)
  • 親会社列参照(会社表のID番号)
  • 2社目参照(企業表のID番号)
  • 会社名(子会社名)

それを行うクエリを作成するにはどうすればよいですか?

助けてください!!

4

1 に答える 1

0

クエリで case ステートメントを使用して、ロジックを一致させることができます。これは近いはずだと思います:

select m.id,
      case
           -- has both, use child
           when p.id is not null and s.id is not null then m.company
           -- has parent not second, use parent
           when p.id is not null and s.id is null then p.company
           -- has second not parent, use second
           when p.id is null and s.id is null then p.company
           -- has neither, not sure what you want to do
           when p.id is null and s.id is null then 'None'
      end as company
from movies as m
left join companies as s
    on m.second_company_id = s.id
left join companies as p
    on m.parent_company_id = p.id
于 2013-06-06T21:21:30.457 に答える