0

1 対多のインデックスに関連する 2 つのテーブルがあります。このシナリオは初めてで、データを取得するための優れたスマートなクエリを見つけようとしています。

テーブル名: 質問

構造:

id poll_id question

テーブル名: 回答

構造:

id question_id answer

テーブルanwsersのquestion_idは、質問テーブルのidの前置キーです。

poll_idを指定して、すべての質問 (およびそれぞれの回答オプション) を取得する必要があります。これまでのところ、次のクエリがあります。

'SELECT * FROM polls_questions pq, polls_answers WHERE `poll_id` = ' . $pid

しかし、返された配列はひどく非効率的で、データが何度も繰り返されます。

4 つの質問の投票では、35 の要素が得られます。

Array
(
    [0] => Array
        (
            [id] => 1
            [poll_id] => 1
            [question] => Domanda 1
            [atype] => 0
            [question_id] => 1
            [answer] => Risposta 1 domanda 1
        )

    [1] => Array
        (
            [id] => 1
            [poll_id] => 1
            [question] => Domanda 2
            [atype] => 0
            [question_id] => 1
            [answer] => Risposta 1 domanda 1
        )

    [2] => Array
        (
            [id] => 1
            [poll_id] => 1
            [question] => Domanda 3
            [atype] => 0
            [question_id] => 1
            [answer] => Risposta 1 domanda 1
        )

    [3] => Array
        (
            [id] => 1
            [poll_id] => 1
            [question] => Domanda 4
            [atype] => 0
            [question_id] => 1
            [answer] => Risposta 1 domanda 1
        )

    [4] => Array
        (
            [id] => 2
            [poll_id] => 1
            [question] => Domanda 1
            [atype] => 0
            [question_id] => 1
            [answer] => Risposta 2 domanda 1 (F: Domanda 3)
        )

    [5] => Array
        (
            [id] => 2
            [poll_id] => 1
            [question] => Domanda 2
            [atype] => 0
            [question_id] => 1
            [answer] => Risposta 2 domanda 1 (F: Domanda 3)
        )

    [6] => Array
        (
            [id] => 2
            [poll_id] => 1
            [question] => Domanda 3
            [atype] => 0
            [question_id] => 1
            [answer] => Risposta 2 domanda 1 (F: Domanda 3)
        )

    [7] => Array
        (
            [id] => 2
            [poll_id] => 1
            [question] => Domanda 4
            [atype] => 0
            [question_id] => 1
            [answer] => Risposta 2 domanda 1 (F: Domanda 3)
        )

    [8] => Array
        (
            [id] => 3
            [poll_id] => 1
            [question] => Domanda 1
            [atype] => 0
            [question_id] => 2
            [answer] => Risposta 1 domanda 2
        )

    [9] => Array
        (
            [id] => 3
            [poll_id] => 1
            [question] => Domanda 2
            [atype] => 0
            [question_id] => 2
            [answer] => Risposta 1 domanda 2
        )

    [10] => Array
        (
            [id] => 3
            [poll_id] => 1
            [question] => Domanda 3
            [atype] => 0
            [question_id] => 2
            [answer] => Risposta 1 domanda 2
        )

    [11] => Array
        (
            [id] => 3
            [poll_id] => 1
            [question] => Domanda 4
            [atype] => 0
            [question_id] => 2
            [answer] => Risposta 1 domanda 2
        )

    [12] => Array
        (
            [id] => 4
            [poll_id] => 1
            [question] => Domanda 1
            [atype] => 0
            [question_id] => 2
            [answer] => Risposta 2 domanda 2
        )

    [13] => Array
        (
            [id] => 4
            [poll_id] => 1
            [question] => Domanda 2
            [atype] => 0
            [question_id] => 2
            [answer] => Risposta 2 domanda 2
        )

    [14] => Array
        (
            [id] => 4
            [poll_id] => 1
            [question] => Domanda 3
            [atype] => 0
            [question_id] => 2
            [answer] => Risposta 2 domanda 2
        )

    [15] => Array
        (
            [id] => 4
            [poll_id] => 1
            [question] => Domanda 4
            [atype] => 0
            [question_id] => 2
            [answer] => Risposta 2 domanda 2
        )

    [16] => Array
        (
            [id] => 5
            [poll_id] => 1
            [question] => Domanda 1
            [atype] => 0
            [question_id] => 3
            [answer] => Risposta 1 domanda 3
        )

    [17] => Array
        (
            [id] => 5
            [poll_id] => 1
            [question] => Domanda 2
            [atype] => 0
            [question_id] => 3
            [answer] => Risposta 1 domanda 3
        )

    [18] => Array
        (
            [id] => 5
            [poll_id] => 1
            [question] => Domanda 3
            [atype] => 0
            [question_id] => 3
            [answer] => Risposta 1 domanda 3
        )

    [19] => Array
        (
            [id] => 5
            [poll_id] => 1
            [question] => Domanda 4
            [atype] => 0
            [question_id] => 3
            [answer] => Risposta 1 domanda 3
        )

    [20] => Array
        (
            [id] => 6
            [poll_id] => 1
            [question] => Domanda 1
            [atype] => 0
            [question_id] => 3
            [answer] => Risposta 2 domanda 3 (F: END POLL)
        )

    [21] => Array
        (
            [id] => 6
            [poll_id] => 1
            [question] => Domanda 2
            [atype] => 0
            [question_id] => 3
            [answer] => Risposta 2 domanda 3 (F: END POLL)
        )

    [22] => Array
        (
            [id] => 6
            [poll_id] => 1
            [question] => Domanda 3
            [atype] => 0
            [question_id] => 3
            [answer] => Risposta 2 domanda 3 (F: END POLL)
        )

    [23] => Array
        (
            [id] => 6
            [poll_id] => 1
            [question] => Domanda 4
            [atype] => 0
            [question_id] => 3
            [answer] => Risposta 2 domanda 3 (F: END POLL)
        )

    [24] => Array
        (
            [id] => 7
            [poll_id] => 1
            [question] => Domanda 1
            [atype] => 0
            [question_id] => 4
            [answer] => Risposta 1 domanda 4
        )

    [25] => Array
        (
            [id] => 7
            [poll_id] => 1
            [question] => Domanda 2
            [atype] => 0
            [question_id] => 4
            [answer] => Risposta 1 domanda 4
        )

    [26] => Array
        (
            [id] => 7
            [poll_id] => 1
            [question] => Domanda 3
            [atype] => 0
            [question_id] => 4
            [answer] => Risposta 1 domanda 4
        )

    [27] => Array
        (
            [id] => 7
            [poll_id] => 1
            [question] => Domanda 4
            [atype] => 0
            [question_id] => 4
            [answer] => Risposta 1 domanda 4
        )

    [28] => Array
        (
            [id] => 8
            [poll_id] => 1
            [question] => Domanda 1
            [atype] => 0
            [question_id] => 4
            [answer] => Risposta 2 domanda 4
        )

    [29] => Array
        (
            [id] => 8
            [poll_id] => 1
            [question] => Domanda 2
            [atype] => 0
            [question_id] => 4
            [answer] => Risposta 2 domanda 4
        )

    [30] => Array
        (
            [id] => 8
            [poll_id] => 1
            [question] => Domanda 3
            [atype] => 0
            [question_id] => 4
            [answer] => Risposta 2 domanda 4
        )

    [31] => Array
        (
            [id] => 8
            [poll_id] => 1
            [question] => Domanda 4
            [atype] => 0
            [question_id] => 4
            [answer] => Risposta 2 domanda 4
        )

    [32] => Array
        (
            [id] => 9
            [poll_id] => 1
            [question] => Domanda 1
            [atype] => 0
            [question_id] => 4
            [answer] => Risposta 3 domanda 4
        )

    [33] => Array
        (
            [id] => 9
            [poll_id] => 1
            [question] => Domanda 2
            [atype] => 0
            [question_id] => 4
            [answer] => Risposta 3 domanda 4
        )

    [34] => Array
        (
            [id] => 9
            [poll_id] => 1
            [question] => Domanda 3
            [atype] => 0
            [question_id] => 4
            [answer] => Risposta 3 domanda 4
        )

    [35] => Array
        (
            [id] => 9
            [poll_id] => 1
            [question] => Domanda 4
            [atype] => 0
            [question_id] => 4
            [answer] => Risposta 3 domanda 4
        )

)

これは、質問用と回答用の 2 つのクエリを使用するだけで解決できます。これにより、2 つの配列が得られますが、テーブル間の関係には意味がありません。データベースにクエリを実行し、最適な方法で質問と回答を取得するにはどうすればよいですか?

4

2 に答える 2

0

どの列のテーブルも結合していないため、デカルトの結果セットが得られます。

'SELECT * 
FROM polls_questions pq
INNER JOIN polls_answers pa
  on pq.id = pa.question_id 
WHERE `poll_id` = ' . $pid

JOIN質問 ID でテーブルを結合する構文を使用する必要があります。これは、回答のある質問のみを返すようにするためです。そうしないと、すべての回答で各質問が返されます。

INNER JOIN両方のテーブル間で一致する行を返す 2 つのテーブル間を使用しました。回答に一致する行がなくてもすべての質問を返したい場合は、LEFT JOIN.

結合構文を理解するのに助けが必要な場合は、結合の視覚的な説明をご覧ください。

于 2013-02-06T16:44:34.883 に答える
0
'SELECT * 
FROM polls_questions pq, polls_answers 
WHERE `poll_id` = ' . $pid . ' AND pq.id = polls_answers.question_id'
于 2013-02-06T16:45:20.417 に答える