我有三个表,第一个存储歌曲数据,第二个存储播放列表名称和所有者ID,第三个表格将歌曲ID链接到他们所属的播放列表。
问题是如何在限制对服务器的请求数量的同时检索用户的播放列表及其包含的歌曲,有没有办法在单个查询中检索这些信息?
到目前为止,我的PHP代码就在我的帖子的末尾。
任何帮助或建议都会很棒,我真的为此疯狂:)
由于
表1 song_main
ID song_artist song_album song_url
1 woops foo1 www.
2 bob foo2 www.
表2 playlists
ID title owner_id
1 myplaylist 20
1 myplaylist1 23
表3 song_in_list
ID song_id playlist_id
1 1 20
1 1 23
这是我的PHP代码,我基本上想要先获取播放列表ID并命名,然后运行第二个查询以获取每个播放列表包含的歌曲ID,然后运行第三个查询以获取每个歌曲信息。我只是不知道我在做什么是对的......
//Getting user's playlists
$raw_resultsplaylists = mysql_query("SELECT * FROM playlists
WHERE (`owner_id` = '$userid')" ) or die(mysql_error());
//fetch the query and adding them to vars
$counter = 1;
while($resultsplaylists = mysql_fetch_array($raw_resultsplaylists))
{
${"playlistname" . $counter} = $resultsplaylists['id'];
${"playlistid" . $counter} = $resultsplaylists['playlistid'];
$counter= $counter+1;
}
答案 0 :(得分:2)
我创建了这些表:
CREATE TABLE IF NOT EXISTS `song_main` (
`ID` varchar(255),
`song_artist` varchar(255),
`song_album` varchar(255),
`song_url` varchar(255),
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `song_in_list` (
`ID` varchar(255),
`song_id` varchar(255),
`playlist_id` varchar(255),
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `playlists` (
`ID` varchar(255),
`title` varchar(255),
`owner_id` varchar(255),
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
插入此数据:
INSERT INTO `song_main` (`ID`, `song_artist`, `song_album`, `song_url`) VALUES
(1, 'Artist 1', 'Album 1', 'www.url1.com'),
(2, 'Artist 2', 'Album 2', 'www.url2.com'),
(3, 'Artist 3', 'Album 3', 'www.url3.com'),
(4, 'Artist 4', 'Album 4', 'www.url4.com');
INSERT INTO `song_in_list` (`ID`, `song_id`, `playlist_id`) VALUES
(1, 1, 1),
(2, 2, 1),
(3, 3, 1),
(4, 2, 2),
(5, 4, 2),
(6, 4, 3);
INSERT INTO `playlists` (`ID`, `title`, `owner_id`) VALUES
(1, 'Playlist 1 for owner 1', 1),
(2, 'Playlist 2 for owner 1', 1),
(3, 'Playlist 1 for owner 2', 2);
然后我可以使用此查询从所有播放列表中获取用户ID 1的所有歌曲:
SELECT songs.*, playlists.title as `playlist_title`
FROM song_main AS songs
JOIN song_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 = 1
结果是:
ID song_artist song_album song_url playlist_title
1 Artist 1 Album 1 www.url1.com Playlist 1 for owner 1
2 Artist 2 Album 2 www.url2.com Playlist 1 for owner 1
3 Artist 3 Album 3 www.url3.com Playlist 1 for owner 1
2 Artist 2 Album 2 www.url2.com Playlist 2 for owner 1
4 Artist 4 Album 4 www.url4.com Playlist 2 for owner 1
您也可以轻松添加播放列表ID:
SELECT songs.*, playlists.title as `playlist_title`
FROM song_main AS songs
JOIN song_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 = 1
AND playlists.ID = 1
答案 1 :(得分:0)
查询将是(用您需要的字段替换*并将WHERE
子句修复为您的PHP变量):
SELECT *
FROM playlists
JOIN song_in_list
ON playlists.id = song_in_list.playlist_id
JOIN song_main
ON song_main.id = song_in_list.song_id
WHERE playlists.owner_id = 'foo';