2015-05-20 24 views
0

我有以下從3個表中抽取的查詢。每行最終會有1個家庭,但每個家庭都有多個孩子。我希望能夠顯示家庭排中的所有兒童年齡。我想打開另一個連接/查詢,但認爲有一個更聰明的方法。在1行內顯示多行數據MySQL PHP

查詢:

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 

回答

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

0

關注這個問題:Nested Array from multiple tables

請參閱問題中的第二個選項,它解釋瞭如何從JOIN查詢中減去數據。

P.S. 我沒有問過自己的一個問題,用你在這裏做什麼來實現。如果您需要關於如何實現在這裏更鉛,問評論...

這裏是實現它在你的代碼的方式(請注意您應該通過「fam_firstname」你的連接查詢,此碼爲你工作):

/* 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); 
+0

這真棒,謝謝。給它一個鏡頭會讓你知道它是怎麼回事 – Klav

+0

@Klav,我已經在我的回答中添加了一個爲你的查詢指定的代碼實現,請參閱上面的...不要忘記用「family_name」命令你的查詢,在爲了正常工作 –

+0

考慮到mysql已經提供了這個功能(請參閱我的答案),不知道爲什麼你想在php中這樣做... –