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 つの配列が得られますが、テーブル間の関係には意味がありません。データベースにクエリを実行し、最適な方法で質問と回答を取得するにはどうすればよいですか?