0

基本上,我有三张桌子。我有一个项目表、一个问题表和一个答案表。一个项目可以有很多问题,一个问题可以有很多答案。在问题 ID 上使用 PDO 和 LEFT JOIN,我如何将带有答案的评论转换为多维数组,以便结构如下所示:

[Question] => Array
    (
        [id] => 1
        [question] => 'Random Question'
        [askedBy] => 123
        [answer] => Array
            (
              [0] => Array
                 (
                   [id] => 1
                   [answer] => 'An Answer'
                   [answeredBy] => 123
                 )
               [1] => Array
                 (
                   [id] => 1
                   [answer] => 'Another Answer'
                   [answeredBy] => 123
                 )
            )
      )

最终代码(返回我想要的)

$questions = array();
$questionCounter = 0;
$questionID = NULL;


$STH = $DBH->query("SELECT `project_question`.`id` AS question_id, `project_question`.`question`, `project_question`.`userID` AS askedBy, 
                               `project_question`.`created` AS question_created, `project_answer`.`id` AS answer_id, 
                               `project_answer`.`answer`, `project_answer`.`userID` AS answeredBy, 
                               `project_answer`.`accepted`, `project_answer`.`created` AS answer_created
                          FROM `project_question`
                     LEFT JOIN `project_answer`
                            ON `project_question`.`id` = `project_answer`.`questionID`
                         WHERE `project_question`.`projectID` = $args[0]
                           AND `project_question`.`projectPhase` = 2");

    while($row = $STH->fetch(PDO::FETCH_ASSOC)){
      if($row['question_id'] !== $questionID){
        $questions[$questionCounter] = array(
            'id' => $row['question_id'],
            'question' => $row['question'],
            'userID' => $row['askedBy'],
            'created' => $row['question_created'],
            'answers' => array()                  
        );
        array_push($questions[$questionCounter]['answers'], 
             array(
              'id' => $row['answer_id'],
              'answer' => $row['answer'],
              'userID' => $row['answeredBy'],
              'accepted' => $row['accepted'],
              'created' => $row['answer_created']
        ));
        $questionCounter++;
        $questionID = $row['question_id'];
      } else {
        array_push($questions[$questionCounter - 1]['answers'], 
             array(
              'id' => $row['answer_id'],
              'answer' => $row['answer'],
              'userID' => $row['answeredBy'],
              'accepted' => $row['accepted'],
              'created' => $row['answer_created']
        ));
      }          
    }
4

1 回答 1

0

在一个查询中,您可以请求所有答案、将问题加入答案并将项目加入问题。

  1. 创建空数组$projects = array()
  2. 遍历每个结果行foreach ($rows as $row)
  3. 如果项目尚不存在,则将项目及其数据添加到数组中(使用项目 ID 作为键)if (!isset($projects[$row->project_id])) { $projects[$row->project_id] = array('col1' => $row->col1,'questions' => array()); }
  4. 将问题及其数据添加到项目问题数组中,如果它尚不存在(使用问题 ID 作为键)if (!isset($projects[$row->project_id]['questions'][$row->question_id])) { $projects[$row->project_id]['questions'][$row->question_id] = array('col2' => $row->col2,'answers' => array()); }
  5. 将答案及其数据添加到项目问题答案数组(使用答案 ID 作为键)$projects[$row->project_id]['questions'][$row->question_id]['answers'][$row->answer_id] = array('col3' => $row->col3);

但正如您所理解的,您将在该查询中获得许多无用的信息。您可以使用一个查询,仅获取项目,遍历它们,在每个周期中将数据添加到数组并再次查询以获取具有特定 project_id 的问题,遍历它们,在每个周期中将数据添加到数组并再次查询以获得答案具有特定的 question_id,遍历它们,并将数据添加到数组中。

但是如果我重新考虑这一点,我认为 MySQL 会比 PHP 运行得更快,即使返回一堆无用的数据(我的意思是,重复有关项目和问题的数据)和 1 个查询与来自一个客户端的可能 50 个查询要好得多,所以我建议最好使用第一种方法。

如果没有关于数据库表和列的更多信息,除了伪代码算法之外,你不会从我这里得到任何东西。

编辑:可能的 MySQL 选择查询:

SELECT 
    a.id answer_id, a.answer, a.answeredBy, 
    q.id question_id, q.question, q.askedBy, 
    p.id project_id, p.title
FROM answer a
LEFT JOIN question q ON q.id = a.question_id
LEFT JOIN project p ON p.id = q.project_id

对于表结构

  • 项目 -> | 编号 | 标题 |
  • 问题-> | 编号 | 问题 | 询问者 | 项目 ID |
  • 答案-> | 编号 | 答案 | 回答者 | 问题ID |
于 2013-11-08T21:42:02.577 回答