MYSQL 中的 Php 聚合函数不起作用
$result = mysql_query("SELECT avg(r.rate) FROM rate r where ImgName='1'");
这个 php 不工作。
最初我的代码是
<?php
$con = mysql_connect("localhost","root","sql");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("photogallery", $con);
$result = mysql_query("SELECT avg(r.rate) FROM rate r ");
echo "<table border='0' cellspacing='5'>";
echo "<th> Average Rating </td>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td> " . $row['rate'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
上面没有显示任何输出。
但修改代码即可工作。
$result = mysql_query("从利率 r 中选择 r.rate");
但我想聚合函数
提前致谢
$result = mysql_query("SELECT avg(r.rate) FROM rate r where ImgName='1'");
this php is not working.
Originally my code is
<?php
$con = mysql_connect("localhost","root","sql");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("photogallery", $con);
$result = mysql_query("SELECT avg(r.rate) FROM rate r ");
echo "<table border='0' cellspacing='5'>";
echo "<th> Average Rating </td>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td> " . $row['rate'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
the above is not showing any out put.
but modify code i.e. then its workin.
$result = mysql_query("SELECT r.rate FROM rate r ");
but i want to aggregate function
thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用别名:
然后输出:
you can use an alias:
and then output:
您的查询生成一个标量而不是一组行。如果您想获得每个项目的平均速率,那么您应该执行以下操作:
是的,如果您想按列名称获取值,您应该使用别名,如 knittl 提到的。
Your query is producing a scalar rather than a set of rows. If you want to get the average rate per item then you should do something like:
And yes, if you want to fetch the value by column name, you should use an alias, like knittl mentioned.