2011-04-12 141 views
0

我似乎無法正確顯示此查詢。下面是代碼:正確顯示MySQL「GROUP BY」結果

$select_stats = "SELECT 
    c.ResponderID 
    , COUNT(MsgID) 
    , a.SubscriberID 
    , CanReceiveHTML 
    , EmailAddress 
FROM InfResp_msglogs AS a 
    , InfResp_subscribers AS b 
    , InfResp_responders AS c 
WHERE b.CanReceiveHTML = 1 
    AND c.owner='".$_SESSION['logged_user_id']."' 
    AND c.ResponderID = b.ResponderID 
    AND b.SubscriberID = a.SubscriberID 
GROUP BY c.ResponderID"; 

//echo $select_result; 

while($row = mysql_fetch_array($select_result)) 
{ 
    $messages = $row['COUNT(MsgID)']; 
    $campaign = $row['ResponderID']; 
    $subscriber = $row['SubscriberID']; 
    $subscriber_email = $row['EmailAddress']; 

    echo "<br>Campaign = ". $campaign ." 
      <br>Sent emails count = ". $messages ." 
      <br>Subscriber Nr. = ". $subscriber ." 
      <br>Subscriber Email = ". $subscriber_email ."<br>"; 
    } 

這是輸出:

Campaign = 10 
Sent emails count = 109 
Subscriber Nr. = 95 
Subscriber Email = [email protected] 

Campaign = 11 
Sent emails count = 16 
Subscriber Nr. = 97 
Subscriber Email = [email protected] 

由於有下運動三個不同的用戶及10號之一的,​​在數據​​庫活動11號(每個都有自己的電子郵件地址當然),我想輸出爲:

Campaign = 10 
Sent emails count = 109 
Subscriber Nr. = 92 
94 
95 
Subscriber Email = [email protected] 
[email protected] 
[email protected] 

Campaign = 11 
Sent emails count = 16 
Subscriber Nr. = 97 
Subscriber Email = [email protected] 

謝謝。

回答

3

需要group_concat此:

... SELECT c.ResponderID, COUNT(MsgID), a.SubscriberID, CanReceiveHTML, 
group_concat(EmailAddress ORDER BY EmailAddress DESC SEPARATOR ' ') ... 
+1

但group_concat_max_len'的'提防 – Emmerman 2011-04-12 13:47:06