2013-11-15 74 views
0

我有一個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表2

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

等等

+3

在哪裏查詢? – Raptor

+0

你可以使用GROUP_BY –

+0

請問我可以在不同的html表格中顯示不同的數組嗎? – anto0522

回答

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 
} 
+0

感謝您的回答,它似乎正是我所需要的,您能否向我解釋foreach的用途? – anto0522

+0

它確實如名稱所示。對於數組中的每個元素都做了一些事情。更多信息,像往常一樣,在手冊中:http://us1.php.net/manual/en/control-structures.foreach.php – Babblo

+0

因此,我可以使用foreach顯示不同的HTML表格中的元素吧?再次感謝 – anto0522

0

有沒有 「陣列」 中的SQL。這是所有的行和列。你想要的是,也許是:

SELECT * FROM table_name WHERE pname='foo1' 

或許:

SELECT * FROM table_name ORDER BY pname 

在那裏你可以做你的應用程序中的分裂。

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()); 
+0

謝謝你,但我不知道當我將查詢發送到Mysql playlist_title。 – anto0522