Jump to content

Group By


MrXHellboy

Recommended Posts

I have a simple query:

SELECT name, skill FROM members as a LEFT JOIN skills as b ON a.id = b.member_id

 

For instance, we have 1 name with 4 skills. Within a loop it will result in 4 times the name, followed with the skill.

 

But i want to have 1 name with all of the skills. Not 4 times the name with each time a other skill. Group By name results in 1 single name with 1 single skill while he has 4 skills.

 

Someone has a solution ?

Link to comment
https://forums.phpfreaks.com/topic/231285-group-by/
Share on other sites

The post above here will describe my tables... This is the result now:

Laurens Skills: Football

Max Skills: Football

Laurens Skills: Football

Max Skills: Swimming

Max Skills: Football

Laurens Skills: Swimming

Laurens Skills: Football

Laurens Skills: Football

Laurens Skills: Swimming

 

As you can see (dont pay attention to the skills), just a test, it will result in name -> skill, done with the following code:

$Query = "SELECT name, skill FROM members as a INNER JOIN skills as b ON a.id = b.member_id";

$result = mysql_query($Query)
                    or die ('Something went wrong!');
                    
                    
while ($row = mysql_fetch_object($result))
{
    echo $row->name.' Skills: '. $row->skill.'<br />';
}

 

I want to have:

Laurens: -> All the skills

Max: -> All the skills

 

Not name and skill, time after time

Link to comment
https://forums.phpfreaks.com/topic/231285-group-by/#findComment-1190422
Share on other sites

Untested:

 

$sql = mysql_query("SELECT m.id, m.name, s.skill FROM skills s LEFT JOIN members m ON(s.member_id = m.id)");
$skills = array();
while($row = mysql_fetch_assoc($sql)){
    $skills[$row['id']][$row['name']][] = $row['skill'];
}

foreach($skills as $name => $skill){
    echo $name . implode(', '$skill[$name]);
}

Link to comment
https://forums.phpfreaks.com/topic/231285-group-by/#findComment-1190452
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.