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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.