PHP-mysql怎样查询聚合函数

 

题目要求:

  查看游戏总分与平均分等

    image.png

    (1)在index.php页面,使用 PDO 方式,链接操作 MySQL 数据库。    

    (2)查看游戏分数表game_score,用户Id(user_id)等于1的用户游戏总分,

             并将获得的总分赋予变量 $total_score

    (3)查看游戏分数表game_score,游戏Id(game_id) 等于1的游戏平均分,

             并将获得平均分赋予变量 $average_score

    (4)查看游戏分数表game_score,最高分值,并将获得最高分赋予变量 

             $max_score

    (5)查看游戏分数表game_score,最低分值,并将获得最低分赋予变量 

             $min_score

    (6)输出查询出来的结果:

                    "<p>总分 :{$total_score}</p>";

                    "<p>平均分:{$average_score}</p>";

                    "<p>最高分:{$max_score}</p>";

                    "<p>最低分:{$min_score}</p>";

    数据表game_score说明:

        id                  序号,

        game_id        游戏Id,

        user_id          用户Id,

        score             游戏分数,

 

 

 

 

 

 

我的代码:

<style media="screen">
  p{
    margin: 0px auto;
    padding: 0px;
    width: 280px;
    height: 40px;
    line-height: 40px;
    color: #fff;
    padding-left: 68px;
    background: #4caf50;
    border-bottom: 1px solid #118816;
  }

</style>
<?php 
header("content-type:text/html;charset=utf-8");
//$url,$user,$pwd是自动生成的数据库相关信息,不能修改
//连接数据库时不需要写端口号
$url = "mysql:host=mysql;dbname=database_25523_19_91532";//数据库ip和库名
$user = "25523_19_91532";//数据库用户
$pwd = "79ff8b565258025a2ac0f21d5b28d6d5";//数据库密码

/**
 * 题中要输出的内容:
 *(1)"<p>总分 :{$total_score}</p>";
 *(2)"<p>平均分:{$average_score}</p>";
 *(3)"<p>最高分:{$max_score}</p>";
 *(4)"<p>最低分:{$min_score}</p>";
 * 
 */

$total_score="select sum(user_id) where id=1 from game_score"; 
$average_score="select avg(game_id) where id=1 from game_score"; 
$max_score="select max(score) from game_score"; 
$min_score="select min(score) from game_score"; 

echo "<p>总分 :{$total_score}</p>";
echo "<p>平均分:{$average_score}</p>";
echo "<p>最高分:{$max_score}</p>";
echo "<p>最低分:{$min_score}</p>";
$conn = new PDO($url,$user,$pwd);
 

要执行SQL语句获取结果,不是单单打印sql语句,而且sql语句也有问题,看下面的

<style media="screen">
  p{
    margin: 0px auto;
    padding: 0px;
    width: 280px;
    height: 40px;
    line-height: 40px;
    color: #fff;
    padding-left: 68px;
    background: #4caf50;
    border-bottom: 1px solid #118816;
  }

</style>
<?php 
header("content-type:text/html;charset=utf-8");
//$url,$user,$pwd是自动生成的数据库相关信息,不能修改
//连接数据库时不需要写端口号
$url = "mysql:host=mysql;dbname=database_25523_19_91532";//数据库ip和库名
$user = "25523_19_91532";//数据库用户
$pwd = "79ff8b565258025a2ac0f21d5b28d6d5";//数据库密码

/**
 * 题中要输出的内容:
 *(1)"<p>总分 :{$total_score}</p>";
 *(2)"<p>平均分:{$average_score}</p>";
 *(3)"<p>最高分:{$max_score}</p>";
 *(4)"<p>最低分:{$min_score}</p>";
 * 
 */

$total_score="select sum(score)  from game_score where user_id=1"; 
$average_score="select avg(score)  from game_score where game_id=1"; 
$max_score="select max(score) from game_score"; 
$min_score="select min(score) from game_score"; 

$conn = new PDO($url,$user,$pwd);
$r=$conn->query($total_score)->fetch();
$total_score= $r[0];

$r=$conn->query($average_score)->fetch();
$average_score= $r[0];

$r=$conn->query($max_score)->fetch();
$max_score= $r[0];

$r=$conn->query($min_score)->fetch();
$min_score= $r[0];


echo "<p>总分 :{$total_score}</p>";
echo "<p>平均分:{$average_score}</p>";
echo "<p>最高分:{$max_score}</p>";
echo "<p>最低分:{$min_score}</p>";