【问题标题】:Display multiple row data within 1 row MySQL PHP在 1 行 MySQL PHP 中显示多行数据
【发布时间】:2015-05-20 17:36:52
【问题描述】:

我有以下从 3 个表中提取的查询。我将最终每行有 1 个家庭,但每个家庭有多个孩子。我希望能够显示家庭行内的所有儿童年龄。我想过打开另一个连接/查询,但发现有更聪明的方法。

查询:

SELECT 
    families.*, job.*, children.*, families.first_name AS fam_firstname, children.first_name AS child_firstname
FROM job
    LEFT OUTER JOIN families ON job.fam_id = families.fam_id
    LEFT OUTER JOIN children ON families.fam_id = children.fam_id
WHERE 
    job.published = 2 
GROUP BY job.job_id
ORDER BY job.created_on DESC

循环:

if ($result = $mysqli->query($query)) {

    $from = new DateTime($row['dob']);
    $to   = new DateTime('today');

    while ($row = $result->fetch_assoc()) {
         echo '<tr>';
         echo '<td>' .$row['fam_firstname']. '</td>';
         echo '<td>' .$row['last_name'].'</td>';

         /* Looking to list all children ages. Separate by comma or break  */
         echo '<td>' . $from->diff($to)->y .'</td>';

         echo '</tr>';
    }

    $result->free();
}

期望的输出:

Family First Name  |   Family Last Name   |   Child 1 Age, Child 2 Age

【问题讨论】:

    标签: php mysql loops while-loop


    【解决方案1】:

    你需要使用mysql的group_concat函数来实现:

    SELECT 
        families.*, group_concat(children.age)
    FROM job
        LEFT OUTER JOIN families ON job.fam_id = families.fam_id
        LEFT OUTER JOIN children ON families.fam_id = children.fam_id
    WHERE 
        job.published = 2 
    group by families.fam_id
    

    ORDER BY job.created_on DESC

    【讨论】:

      【解决方案2】:

      关注这个问题:Nested Array from multiple tables

      请参阅问题中的第二个选项,它解释了如何从 JOIN 查询中减去数据。

      附注 这是我问过自己的一个问题,并与您在这里尝试做的事情一起实现。如果您需要更多关于如何在此处实施的指导,请在 cmets 中询问...

      这是在您的代码中实现它的一种方法(请注意,您应该按“fam_firstname”为您的 JOIN 查询排序,以便此代码为您工作):

      /* init temp vars to save current family's data */
      $current = null;
      $fam_firstname = null;
      $children = array();
      while ($row = mysql_fetch_assoc($result))
      {
          /*
             if the current id is different from the previous id:
             you've got to a new family.
             print the previous family (if such exists),
             and create a new one
          */
          if ($row['fam_firstname'] != $fam_firstname )
          {
              // in the first iteration,
              // current (previous family) is null,
              // don't print it
              if ( !is_null($current) )
              {
                  $current['children'] = $children;
                  /*
                      Here you print the whole line
                      I'm just dumping it all here, but you can print
                      it more nicer...
                  */
                  var_dump($current);
                  $current = null;
                  $fam_firstname = null;
                  $children = array();
              }
      
              // create a new family
              $current = array();
              $current['fam_firstname'] = $row['fam_firstname'];
              /*
                  Add more columns value here...
              */
              // set current as previous id
              $fam_firstname = $current['fam_firstname'];
          }
      
          // you always add the phone-number 
          // to the current phone-number list
          $children[] = $row['child_firstname'] . " is " . $row['child_age'] . " years old";
          }
      }
      
      // don't forget to print the last family (saved in "current")
      if (!is_null($current))
          /*
                  Here you print the whole line
                  I'm just dumping it all here, but you can print
                  it more nicer...
          */
          var_dump($current);
      

      【讨论】:

      • 这太棒了,谢谢。试一试会让你知道它是怎么回事
      • @Klav,我在答案中添加了为您的查询指定的代码实现,见上文...不要忘记也按“family_name”订购您的查询,以便它正常工作
      • 考虑到mysql已经提供了这个功能(见我的回答),不知道为什么要在php中做这个...
      • 1.谢谢!我不知道那个功能(group_concat),所以我也从这里学到了一些东西。 2.如果他只想显示单列数据,“group_concat”是最好的。但是如果他想显示更多的列数据,例如:"{$kidFirstName}-{$kidLastName}'s age is: {$kidAge}",和/或在中间添加一些设计 HTML 标记,那么最好将设计留给 PHP,并将数据获取到 SQL?我不确定哪个更好,你觉得呢?
      猜你喜欢
      • 1970-01-01
      • 2022-09-28
      • 1970-01-01
      • 2018-12-21
      • 2012-04-15
      • 1970-01-01
      • 2014-02-14
      • 2020-10-13
      • 1970-01-01
      相关资源
      最近更新 更多