1

我有一个具有以下结构的表,我想获得某个album_id&的所有时间的总和disc。我找到了这个http://board.phpbuilder.com/showthread.php?10326769-RESOLVED-time-help它看起来有点好(第一个答案中的第一段代码),但我不知道如何工作我的 mysql...

表结构:

CREATE TABLE IF NOT EXISTS `tracks` (
  `id` int(255) NOT NULL AUTO_INCREMENT,
  `artist_id` int(255) NOT NULL,
  `album_id` int(255) NOT NULL,
  `disc` int(3) NOT NULL,
  `track_no` int(3) NOT NULL,
  `title` varchar(255) NOT NULL,
  `length` time NOT NULL,
  `size` decimal(20,1) NOT NULL,
  `plays` int(255) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `artist_id` (`artist_id`,`album_id`,`disc`,`track_no`,`title`)
) ;

任何帮助是极大的赞赏。我是菜鸟!!!

4

2 回答 2

3

也许这会做。

SELECT album_id, disc, SEC_TO_TIME(SUM(TIME_TO_SEC(length))) AS formatTime
FROM tracks
GROUP BY album_id, disc
HAVING album_id=id AND disc=disc

仅适用于特定专辑和光盘:

SELECT album_id, disc, SEC_TO_TIME(SUM(TIME_TO_SEC(length))) AS formatTime
FROM tracks
WHERE album_id=id AND disc=disc
于 2012-07-27T04:17:20.620 回答
0
//this is the album we want
$album = 24;

//grab all the track lengths and plays from our album
$query = '
SELECT length, plays
FROM tracks
WHERE album_id = "'.$album.'";

//do our mysql query
$result = mysql_query($query) or die(mysql_error());

//now for every result do somthing like add the length of the songs or play count
$length = null;
$plays = null;
while ($row = mysql_fetch_assoc($result)) {
  $length = $length + $row["length"];
  $pays = $plays + $row["plays"];
}

//echo data to user
echo 'The total lenth of this albums is:'.$length.'mins';
echo 'this ablum as been played:'.$plays.'times';
于 2012-07-27T04:35:32.090 回答