cakephp2.1中的数据获取问题

时间:2012-06-15 19:07:07

标签: php mysql cakephp cakephp-2.0

我有简单的播放列表数据库结构......它采用这种格式

sr.no |    album_id |    song_id |    artist_id |    user_id

1.    |    160      |    85      |    40        |    5
2.    |    160      |    85      |    45        |    5
3.    |    160      |    85      |    41        |    6
4.    |    160      |    85      |    44        |    5

表示album_id,song_id和artist_id的组合使完整记录独一无二。我想使用CAKEPHP条件在组中获取记录。例如。我想要这种格式的数据。假设我想在特定部分显示它。

album_id | song_id | artist_id
160      |      85 | 40, 45, 41, 44

我的试用代码就是这个..

        $playlist_condition = array('Tracklist.album_id' => $album_id, 'OR'=> array('Tracklist.status'=> 1, 'Tracklist.user_id'=>$user_id));
        $trackdetails = $this->Tracklist->find('all', array('conditions' => $playlist_condition ));
        $this->set("trackdetails", $trackdetails);

注意递归地我也想找到相关的模型..

我目前的输出是..

Array
(
[0] => Array
    (
        [Tracklist] => Array
            (
                [id] => 140
                [album_id] => 1
                [song_id] => 185
                [artist_id] => 33
                [video_link] => 
                [status] => 1
                [user_id] => 
                [created] => 2012-05-25 10:08:50
                [modified_by] => 
                [modified] => 2012-05-25 10:08:50
            )

        [Album] => Array
            (
                [id] => 1
                [name] => Om, Namah Shivay
                [photo] => 
                [genre] => Devotional
                [copyrights] => 
                [year] => 0000
                [source] => 
                [description] => Its' an devotional Album.
                [download_link] => 
                [status] => 2
                [user_id] => 1
                [created] => 2012-04-06 00:00:00
                [modified_by] => 0
                [modified] => 2012-04-22 09:11:35
            )

        [Song] => Array
            (
                [id] => 185
                [name] => Shad.aab
                [hindi] => अ
                [roman] => a
                [category] => Qawwalli
                [poet_id] => 148
                [status] => 7
                [user_id] => 1
                [created] => 
                [modified_by] => 
                [modified] => 2012-04-22 09:07:48
            )

        [Artist] => Array
            (
                [id] => 33
                [name] => Suresh Vadekar
                [photo] => 
                [description] =>  Suresh Vadekar.
                [status] => 1
                [user_id] => 1
                [created] => 2012-05-20 18:12:38
                [modified_by] => 
                [modified] => 2012-05-20 18:12:39
            )

    )

[1] => Array
    (
        [Tracklist] => Array
            (
                [id] => 138
                [album_id] => 1
                [song_id] => 185
                [artist_id] => 31
                [video_link] => 
                [status] => 1
                [user_id] => 
                [created] => 2012-05-25 10:04:55
                [modified_by] => 
                [modified] => 2012-05-25 10:04:55
            )

        [Album] => Array
            (
                [id] => 1
                [name] => Om, Namah Shivay
                [photo] => 
                [genre] => Devotional
                [copyrights] => 
                [year] => 0000
                [source] => 
                [description] => Its' an devotional Album.
                [download_link] => 
                [status] => 2
                [user_id] => 1
                [created] => 2012-04-06 00:00:00
                [modified_by] => 0
                [modified] => 2012-04-22 09:11:35
            )

        [Song] => Array
            (
                [id] => 185
                [name] => Shad.aab
                [hindi] => अ
                [roman] => a
                [category] => Qawwalli
                [poet_id] => 148
                [status] => 7
                [user_id] => 1
                [created] => 
                [modified_by] => 
                [modified] => 2012-04-22 09:07:48
            )

        [Artist] => Array
            (
                [id] => 31
                [name] => Mehdi Hassan
                [photo] => 
                [description] => Mehdi Hassan Sahaab one of the prominiet singer of Asia...
                [status] => 1
                [user_id] => 1
                [created] => 2012-05-20 18:07:20
                [modified_by] => 
                [modified] => 2012-05-20 18:07:20
            )

    )

[2] => Array
    (
        [Tracklist] => Array
            (
                [id] => 139
                [album_id] => 1
                [song_id] => 185
                [artist_id] => 32
                [video_link] => 
                [status] => 1
                [user_id] => 
                [created] => 2012-05-25 10:04:55
                [modified_by] => 
                [modified] => 2012-05-25 10:04:55
            )

        [Album] => Array
            (
                [id] => 1
                [name] => Om, Namah Shivay
                [photo] => 
                [genre] => Devotional
                [copyrights] => 
                [year] => 0000
                [source] => 
                [description] => Its' an devotional Album.
                [download_link] => 
                [status] => 2
                [user_id] => 1
                [created] => 2012-04-06 00:00:00
                [modified_by] => 0
                [modified] => 2012-04-22 09:11:35
            )

        [Song] => Array
            (
                [id] => 185
                [name] => Shad.aab
                [hindi] => अ
                [roman] => a
                [category] => Qawwalli
                [poet_id] => 148
                [status] => 7
                [user_id] => 1
                [created] => 
                [modified_by] => 
                [modified] => 2012-04-22 09:07:48
            )

        [Artist] => Array
            (
                [id] => 32
                [name] => Jagjit Singh
                [photo] => 
                [description] => Jagjit Singh Sahab my favorite Sahab.
                [status] => 1
                [user_id] => 1
                [created] => 2012-05-20 18:11:07
                [modified_by] => 
                [modified] => 2012-05-20 18:11:07
            )

    )

1 个答案:

答案 0 :(得分:1)

在MySQL中使用GROUP_CONCAT怎么样?

SELECT album_id, artist_id, GROUP_CONCAT(song_id) FROM my_table GROUP BY album_id, artist_id