0

我尝试获取内容 ID,这是查询...

$getIDs = mysql_query("SELECT content_id FROM playlist_content WHERE playlist_id=".$rrow['playlist_id']."") or die(mysql_error());
    while($row = mysql_fetch_array($getIDs)) { 
        $array[] = $row;
}

这是结果

Array
(
    [0] => Array
        (
            [0] => 33
            [content_id] => 33
        )

    [1] => Array
        (
            [0] => 13
            [content_id] => 13
        )

    [2] => Array
        (
            [0] => 8
            [content_id] => 8
        )
)

现在必须从具有此 ID 的第二个表中获取值 注意:ID = content_id

$result =  mysql_query("SELECT SUM( length ) as total FROM content WHERE ID ...

并且所有行的回显总和['length'这个ID在哪里需要支持结果,就像102256等等......

或者有什么更好的方法来得到 sum ?

4

2 回答 2

1

实际上,您可以通过连接两个表在单个查询中执行此操作,

SELECT  a.content_id, SUM(b.length) TotalSum
FROM    playlist_content a
        INNER JOIN content b
            ON a.content_id = b.ID 
WHERE   a.playlist_id = IDHERE
GROUP   BY a.content_id

length查询结果将为您提供每个的总和content_id

要进一步了解有关联接的更多信息,请访问以下链接:

于 2013-06-01T03:57:09.393 回答
1

尝试这个:

$query = "SELECT content_id, " .
" ( select sum (length) from content where id = content_id) as content_length " .
" FROM playlist_content " .
" WHERE playlist_id=".$rrow['playlist_id'];

$getIDs = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($getIDs)) { 
    $array[] = $row;
}
于 2013-06-01T03:58:55.723 回答