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

时间:2015-05-20 17:36:53

标签: php mysql loops while-loop

我有以下查询从3个表中提取。我最终每排1个家庭,但每个家庭有多个孩子。我希望能够向所有家庭成员展示所有年龄段的孩子。我考虑过打开另一个连接/查询,但认为有一种更聪明的方式。

查询:

{
    "noteId": 10,
    "type": "person",
    "noteText": "loves broccoli",
}

循环:

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();
}

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

关注此问题:Nested Array from multiple tables

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

P.S。 我提出了一个问题,我已经问过自己,并实施了您在此尝试做的事情。如果您需要更多关于如何在此实施的主要内容,请在评论中提问......

以下是在您的代码中实现它的方法(注意您应该通过&#34; fam_firstname&#34;来订购您的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);