查询Mysql上的相关表

时间:2013-02-06 16:40:17

标签: mysql

我有两个与一对多索引相关的表。这是我第一次在这种情况下尝试找出一个很好的智能查询来检索数据。

表名: 问题

结构

id poll_id question

表名: 答案

结构

id question_id answer
anwsers 中的

question_id 问题表中 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
        )

)

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

2 个答案:

答案 0 :(得分: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

如果您需要帮助了解联接语法,这里有一个很棒的visual explanation of joins

答案 1 :(得分:0)

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