4

私はSQLを基本的に理解しているので、返信する人には事前に助けと感謝が必要です。

2つのテーブルがあります。1つのテーブルには、それらの見出しの下で選択できる見出しとオプションが含まれています。他のテーブルは、そのテーブルの実際のデータ参照にリンクして、見出しの名前とオプションを参照します。

SQLクエリを実行してこれらのテーブルを結合し、一方のテーブルの親/子IDを参照して、もう一方のテーブルからheading + selectedオプションを取得しようとしていますが、返されるのはID番号だけです。結果として何を取り戻そうとしているのかを説明する画像を作成しました。

この画像はここで説明します:

http://i.imgur.com/hSPvY.jpg

注-上記では、18と20ではないと言っていますが、結果にID番号を表示させることはできますが、親のタイトルと子のタイトルからの正しい情報は表示できません。(サーバーサポート-充電可能なサイト訪問)

これは私がSQLで得たところです:

    SELECT custom_def_organizations.title
    FROM custom_data_organizations
    INNER JOIN organizations
    ON custom_data_organizations.organization_id = organizations.id
    INNER JOIN custom_def_organizations
    ON custom_def_organizations.parent_id = custom_data_organizations.root_field_id 
    AND custom_def_organizations.id = custom_data_organizations.field_id
4

1 に答える 1

3

custom_data_organizationを使用せずに、暗黙の階層を使用して、親と子を結合する最初のクエリ:

SELECT parent.id, child.id
    FROM custom_def_organizations AS parent
    JOIN custom_def_organizations AS child
        ON (child.parent_id = parent.id);

これは戻ります:

18  19
18  20
18  21
18  22
18  23

次に、他の情報を取得します。

SELECT parent.id, child.id, CONCAT(parent.title, ' - ', child.title) AS title
    FROM custom_def_organizations AS parent
    JOIN custom_def_organizations AS child
        ON (child.parent_id = parent.id);

これは戻ります:

18  19  Server Support - Yes
18  20  Server Support - Site Visits Chargeable
18  21  Server Support - Site Visits Included
18  22  ...
18  23

同じ概念ですが、custom_data_organizationsがJOINを駆動します。

SELECT cdo.id, CONCAT(parent.title, ' - ', child.title) AS title
    FROM custom_data_organizations AS cdo
    JOIN custom_def_organizations AS parent
        ON (cdo.root_field_id = parent.id)
    JOIN custom_def_organizations AS child
        ON (cdo.field_id = child.id);

これは戻ります:

    85    Server Support - Site Visits Chargeable
    ...
于 2012-10-04T19:15:32.520 に答える