PHP中的数学

Zer*_*oic 0 php

我是初学者.我有两个问题.

这是我的查询:

$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;
Run Code Online (Sandbox Code Playgroud)
  1. 如何使用一个查询而不是两个查询来创建此代码?

  2. 当我显示百分比

表明

Completion Rate : 32.3529411765% 
Run Code Online (Sandbox Code Playgroud)

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

32.3 %
Run Code Online (Sandbox Code Playgroud)

Flu*_*feh 5

要修改你的sql,请使用:

select 
    SUM(achievenum) as achieve_rows, 
    select SUM(achievenum) as achieve_rows 
from games
Run Code Online (Sandbox Code Playgroud)

要显示结果,您希望首先使用round格式化它:

<?php 
    echo round($completed_rate,1)."%"; 
?>
Run Code Online (Sandbox Code Playgroud)