Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个名为“价格”的简单列,并希望获得所有行的平均数。我有这个代码:
$avg = mysql_query("SELECT AVG(price) FROM books WHERE author='$postname'"); $avgprice = mysql_fetch_assoc($avg);
$avg = mysql_query("SELECT AVG(price) FROM books WHERE author='$postname'");
$avgprice = mysql_fetch_assoc($avg);
当我去输出时:
<?php echo $avgprice['AVG(price)'] ?>
它给了我 250.0000 而不是 250 这是实际数字。为什么我在输出后得到这 4 个小数?
这就是 AVG 函数的工作原理,它返回一个 4 位小数的数字。如果你想四舍五入试试这个:
$avg = mysql_query("SELECT ROUND( AVG(price) ) as avg FROM books WHERE author='$postname'"); $avgprice = mysql_fetch_assoc($avg); <?php echo $avgprice['avg'] ?>