在 1 行内显示多行数据 MySQL PHP

Display multiple row data within 1 row MySQL PHP

我有以下从 3 个表中提取的查询。我最终每行有 1 个家庭,但每个家庭有多个 children。我希望能够显示家庭行中的所有 children 年龄。我考虑过再开一个 connection/query,但发现有更聪明的方法。

查询:

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

回答这个问题:

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

P.S。 这不是我问过自己的问题,它是关于你在这里试图做的事情的实施。如果您需要更多有关如何在此处实施它的线索,请在评论中提问...

这是一种在您的代码中实现它的方法(请注意,您应该按 "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);

需要使用mysqlgroup_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

按 job.created_on 描述排序