0

我有 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
        )

)

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

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在两个表之间使用了一个,它将返回两个表之间的匹配行。如果您想返回所有问题,即使答案中没有匹配的行,那么您需要一个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 回答