Joe
Joe

Reputation: 69

Display MySQL "GROUP BY" results correctly

I cannot seem to get this query displayed properly. Here is the code:

$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>";
     }  

This is the output:

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]

As there are three different subscribers under campaign no.10 and one under campaign no.11 in the db (each with its own email addresses of course), I would like the output to be:

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]

Thanks.

Upvotes: 0

Views: 762

Answers (1)

Ocaso Protal
Ocaso Protal

Reputation: 20237

You need group_concat for this:

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

Upvotes: 3

Related Questions