1

次の構造のテーブルがあり、特定のalbum_id&のすべての時間の合計を取得したいと思いdiscます。私はこのhttp://board.phpbuilder.com/showthread.php?10326769-RESOLVED-time-helpを見つけました、そしてそれは幾分良さそうに見えました(最初の答えのコードの最初の断片)、しかし私はそれを動かす方法を理解することができませんi番目の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 に答える