我有2個表與一對多索引相關。這是我第一次在這種情況下,我試圖找出一個很好的智能查詢來檢索數據。查詢Mysql上的相關表格
表名稱:問題
結構:
id poll_id question
表名稱:回答
結構:
id question_id answer
question_id從表anwsers是ID對問題表forehing關鍵。
我需要檢索所有問題(以及相應的答案選項)給出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
)
)
我可以解決這個只用兩個查詢,一個問題,一個是答案,這會給我兩個數組,但這樣做沒有一點對錶之間的關係。我如何查詢數據庫並以最佳方式檢索問題和答案?