Question

I was wondering if anyone ran across the following:

SQL Query:

SELECT s.ID , s.name , s.artist, m.tag_ID, t.name, s.*, m.*, t.*
                FROM lms_song s
                LEFT JOIN lms_map m
                ON s.ID = m.ID
                LEFT JOIN lms_tag t 
                ON m.tag_id = t.ID
                WHERE s.created_by='.$userId.'                                  
                ORDER BY '.$Field.' '.$Direction;

PHP:

<? foreach($this->songs AS $song){ ?>
<tr>
    <td>

        <?php echo $song->songName;?>

    </td>
    <td>
        <?php echo $song->artist;?>
    </td>

    <td>        
        <?php echo $song->tagname;?>

    </td>
</tr>

It currently displays all of the "songs" listed by the user.

While it does list all of the songs associated with the user, it has multiple songname duplicates since each song name has a tag associated with it.

I tried to simply display "Group By s.name" for the "song name".

However, this limits it to one t.name (tag).

I want to display no duplicates of the song name (s.name) by each artist (s.artist), while displaying all the tags (t.name) associated with it.

Any help is appreciated.

Thanks!

Was it helpful?

Solution

I think you are looking for group_concat() with group by:

SELECT s.ID , s.name , s.artist,
       group_concat(m.tag_ID) as tagids,
       group_concat(t.name) as tagnames
FROM lms_song s LEFT JOIN
     lms_map m
     ON s.ID = m.ID LEFT JOIN 
     lms_tag t 
     ON m.tag_id = t.ID
WHERE s.created_by='.$userId.'
GROUP BY s.ID, s.name , s.artist                                 
ORDER BY '.$Field.' '.$Direction;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top