我有 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
)
)
我可以只使用两个查询来解决这个问题,一个用于问题,一个用于答案,这将给我两个数组,但是这样做没有表之间的关系的意义。如何以最佳方式查询数据库并检索问题和答案?