0

我有三个表,第一个存储歌曲数据,第二个存储带有播放列表名称和所有者 ID 的播放列表,第三个将歌曲 ID 链接到它们所属的播放列表。

问题是如何在限制对服务器的请求数量的同时检索用户的播放列表及其包含的歌曲,有没有办法在单个查询中检索这些信息?

到目前为止,我拥有的 PHP 代码在我的帖子末尾。

任何帮助或建议都会很棒,我真的为此疯狂:)

谢谢

表格1song_main

   ID          song_artist          song_album            song_url           
   1             woops                 foo1                 www.                
   2              bob                  foo2                 www.                 

表 2playlists

  ID             title              owner_id               
   1           myplaylist               20                             
   1          myplaylist1               23                        

表3song_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;
            }
4

2 回答 2

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
于 2013-11-14T15:38:04.987 回答
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';
于 2013-11-14T15:36:03.733 回答