3

I know this is something very easy to do but i have not gotten the correct way to do it. i found something Here but different from what i need and no active contribution yet. Someone kindly assist.

I have a table like this

name |status
-------------
mike |yes
mike |yes
mike |no
mike |ney
john |no
john |ney
john |yes

i want to output something like this

name |status           |total
------------------------------
mike |yes-2,no-1,ney-1 | 4
john |yes-1,no-1,ney-1 | 3

I tried using GROUP_CONCAT like this

    result = mysql_query("SELECT name, GROUP_CONCAT(DISTINCT status) AS status 
FROM table GROUP BY name ");
        while($row = mysql_fetch_array($result)){ 
    $st[] = $row['status'];
            $status=explode(",",$row['status']);
            $total = count($status);
            echo $row['name']."|".$row['status']."|".$total."<br><br>"; }

I would like to get the number of each distinct $row['status'] and if possible, a better way of arriving at $total.

EDIT1

name | yes | no | ney | total
------------------------------
mike |2    |1   |1    | 4
john |1    |1   |1    | 3

This second output was achieved Here

Community
  • 1
  • 1
scylla
  • 124
  • 9

1 Answers1

4

There is no need to use php as you can use pure SQL to get the desired result set:

SELECT name, GROUP_CONCAT(totalPerStatus) AS status, 
       (SELECT COUNT(*) FROM mytable WHERE name = t.name) AS total
FROM (
  SELECT name,      
         CONCAT(status, '-', COUNT(*)) AS totalPerStatus            
  FROM mytable
  GROUP BY name, status ) t
GROUP BY name;

The grouping by name, status performed in the sub-query gives you the count per 'status' per 'name'. Using CONCAT produces the following result set:

name    totalPerStatus
-----------------------
john    ney-1
john    no-1
john    yes-1
mike    ney-1
mike    no-1
mike    yes-2

The outer query uses GROUP_CONCAT on totalPerStatus to produce the required result set.

Demo here

Giorgos Betsos
  • 71,379
  • 9
  • 63
  • 98
  • I trust your Answer should work but pls check, I think i have a syntax error. `$result = mysql_query("SELECT name, GROUP_CONCAT(totalPerStatus) AS status (SELECT COUNT(*) FROM table WHERE name = t.name) AS total FROM (SELECT name,CONCAT(status, '-', COUNT(*)) AS totalPerStatus FROM table GROUP BY name, status ) t GROUP BY name; ") ; while($row = mysql_fetch_array($result)){ echo $row['name']."-".$row['status']."-".$row['totalPerStatus']."-".$row['total']; }` – scylla Jun 27 '15 at 11:54
  • 1
    @scylla There's a comma missing after 'AS status' – Giorgos Betsos Jun 27 '15 at 12:22
  • SQL works great but my echo command returns fatal error as i try to verify the data output in each $row. i.e `$row['name'], $row['status'], $row['totalPerStatus']`. how do i go about this? Fatal error: Cannot use object of type stdClass as array in – scylla Jun 27 '15 at 12:39
  • @scylla I'm afraid I can't help you on this. It's been at least ten year since I last wrote some code in php :( – Giorgos Betsos Jun 27 '15 at 12:55
  • twas an error on my part. was using mysql_fetch_object instead of mysql_fetch_array while fiddling with the code. This is great. – scylla Jun 27 '15 at 13:00
  • But I love to restructure my outcome this way Pls Check the question edit. could not get my fiddle to work. – scylla Jun 27 '15 at 14:12
  • @scylla Well, that's a different question. I think you should write a new post. – Giorgos Betsos Jun 27 '15 at 14:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/81714/discussion-between-scylla-and-giorgos-betsos). – scylla Jun 27 '15 at 14:29