切片Mysql数组

时间:2013-11-15 04:02:55

标签: php mysql

我有一个mysql查询

$raw_resultsplaylists = mysql_query("SELECT songs.*, playlists.title as `playlist_title`
                                 FROM song_main AS songs
                                 JOIN songs_in_list AS playlist_songs 
                                  ON songs.ID = playlist_songs.song_id
                                 JOIN playlists 
                                 ON playlist_songs.playlist_id = playlists.ID
                                WHERE playlists.owner_id = '$userid'") or die(mysql_error());

检索如下信息数组:

  playlist_title      song_name              song_url        song_artist
     -------------------------------------------------------------------        
     foo1               woops                 www.                -
     foo1               blah                  www.                -
     foo2               blop                  www.                -
     foo1               woop                  www.                -
     foo3               bob                   www.                -      
     foo1               dylan                 www.                -

我的问题是如何切片Mysql数组以在单独的数组中获取具有相同playlist_title的所有行,以便我可以在单独的html表中显示它们,例如:

html表1:

 playlist_title      song_name              song_url        song_artist
    -------------------------------------------------------------------        
     foo1               woops                 www.                -
     foo1               blah                  www.                -
     foo1               woop                  www.                -    
     foo1               dylan                 www.                -

html table 2

    playlist_title      song_name           song_url        song_artist
     -------------------------------------------------------------------        
     foo2               blop                  www.                -
     foo2               dylan                 www.                -

等...

3 个答案:

答案 0 :(得分:0)

SQL中没有“数组”。它是所有行和列。你想要的是,也许:

SELECT * FROM table_name WHERE pname='foo1'

或者也许:

SELECT * FROM table_name ORDER BY pname

您可以在应用程序中进行拆分。

答案 1 :(得分:0)

只是为了得出这个想法..

do{

  $groupedItems[$row['pname']][] = $row; //every row with the same pname will be "grouped" in the same array level

}while..

然后......

foreach($groupedItems as $group){
  //bla bla
}

答案 2 :(得分:0)

对于html表1:

mysql_query("SELECT songs.*, playlists.title as `playlist_title`
                             FROM song_main AS songs
                             JOIN songs_in_list AS playlist_songs 
                             ON songs.ID = playlist_songs.song_id
                             JOIN playlists 
                             ON playlist_songs.playlist_id = playlists.ID
                             WHERE playlists.owner_id = '$userid'
                             AND playlist_title='foo1'") or die(mysql_error());