使用PHP MYSQL除以计数结果

时间:2013-04-21 00:19:04

标签: php mysql count divide

我试图除以表格中的行数,在我输入要分割的值的那一分钟,手动使用:

<?php
$result10 = $result10 * 100 / 10 (<-- Result from count)
?>

我认为我需要计算行数并用$ query_result替换10但我已经尝试了这个并且不确定该做什么以及是否可能?

提前致谢

3 个答案:

答案 0 :(得分:1)

作为如何在查询中执行此操作的示例:

select result10 / (select count(*) from <yourquery>)
from <your query>

如果你做一个简单的平均值,那么这可能是最合适的解决方案:

select avg(colx)
from t

答案 1 :(得分:0)

你能做到的另一种方式

 <?php
define('HOST','hostdetails');
define('USER','userdetails');
define('PASS','passdetails');
define('DB','dbdetails');

$mysqli = new mysqli(HOST,USER,PASS,DB);
$sql = "SELECT COUNT(*) FROM table";
$stmt = $mysqli->prepare($sql);
$stmt->execute();
$rowcount = $stmt->num_rows;
//$result10 -> add your variable for result10 here, however you are generating it...

$result10 = (($result10*100)/$rowcount);
?>

答案 2 :(得分:0)

$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);

$result = mysql_query("SELECT * FROM my_table", $link);
$num_rows = mysql_num_rows($result);
$result10 = $result10 * 100 / $num_rows (<-- Result from count)