dqyat62284
2012-12-29 14:25
浏览 58
已采纳

从两个表中获取最高总和值

Here is my code:

$query1 = "select user, sum(column) as total1 from table1 GROUP BY user";
$result = mysql_query(query1);
$row_query1 = mysql_fech_assoc($result);

do{
    $user = $row_query1['user'];
    $query2 = "select names, sum(column1) as total2 from table2 WHERE names ='$user' GROUP BY names";
    $result2 = mysql_query($query2);
    $row_query2 = mysql_fetch_assoc($result2);
    $sum = $row_query1['total1'] + $row_query2['total1'];
    <tr> <?php echo $sum; ?></tr>
}while($row_query1 = mysql_fech_assoc($result));

I need to get the highest value of $sum from this loop. Can anyone help?

图片转代码服务由CSDN问答提供 功能建议

这是我的代码:

  $ query1 =“select  user,sum(column)as total1 from table1 GROUP BY user“; 
 $ result = mysql_query(query1); 
 $ row_query1 = mysql_fech_assoc($ result); 
 
do {
 $ user = $ row_query1 ['  user']; 
 $ query2 =“从表2中选择名称,sum(column1)为total2 WHERE names ='$ user'GROUP BY names”; 
 $ result2 = mysql_query($ query2); 
 $ row_query2 = mysql_fetch_assoc  ($ result2); 
 $ sum = $ row_query1 ['total1'] + $ row_query2 ['total1']; 
&lt; tr&gt;  &lt;?php echo $ sum;  ?&gt;&lt; / tr&gt; 
} while($ row_query1 = mysql_fech_assoc($ result)); 
   
 
 

我需要获得$ sum的最高值 从这个循环。 有人可以帮忙吗?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dtm41506 2012-12-29 14:34
    已采纳

    You can do like this.. take a temporary variable($temp) which can have check upon the sum variable($sum).

         $query1 = "select user, sum(column) as total1 from table1 GROUP BY user";
         $result = mysql_query(query1);
         $row_query1 = mysql_fech_assoc($result);
         $temp = 0;
    
        do{
    
        $user = $row_query1['user'];
    
        $query2 = "select names, sum(column1) as total2 from table2 WHERE names ='$user' GROUP BY names";
        $result2 = mysql_query($query2);
        $row_query2 = mysql_fetch_assoc($result2);
    
        $sum = $row_query1['total1'] + $row_query2['total1'];
    
        if($temp < $sum)
        $temp = sum;
    
        echo "<tr>$sum</tr>";
    
       }while($row_query1 = mysql_fech_assoc($result));
       echo "maximum sum :".$temp;
    
    点赞 打赏 评论
  • dongmu5596 2012-12-29 14:38

    I would advice doing a JOIN instead of performing the sub queries yourself:

    select user, sum(column) + sum(column1) as total
    from table1 
    INNER JOIN table2 ON names = user
    GROUP BY user
    

    The rest should be straightforward in code.

    点赞 打赏 评论

相关推荐 更多相似问题