0

我是这里的初学者。我有两个问题。

这是我的查询:

$result = mysql_query( "select SUM(achievenum) as achieve_rows from games" );
$row = mysql_fetch_object( $result );
$total_achieve = $row->achieve_rows;

$result = mysql_query( "select SUM(achieveget) as get_rows from games" );
$row = mysql_fetch_object( $result );
$get_achieve = $row->get_rows;

$completed_rate = $get_achieve * 100 / $total_achieve;
  1. 如何使用一个查询而不是两个查询来制作此代码?

  2. 当我显示百分比时

表明

Completion Rate : 32.3529411765% 

我需要它只显示前三个数字。

32.3 %
4

2 回答 2

5

要修改您的 sql,请使用以下命令:

select 
    SUM(achievenum) as achieve_rows, 
    select SUM(achievenum) as achieve_rows 
from games

并显示结果,您想先使用圆形对其进行格式化:

<?php 
    echo round($completed_rate,1)."%"; 
?>
于 2012-09-12T06:40:43.050 回答
3

查询将是:

select ROUND(SUM(achieveget)*100/SUM(achievenum),1) as completion_rate from games
于 2012-09-12T06:48:18.713 回答