如何打印出所有课程的成绩

时间:2013-11-02 00:13:24

标签: php mysql

我有一个表名“Table1”,其中包含以下学生记录。

student_id | Name | Math | English | Sience | Class
1            John   100    90        89       std two
2            Simon  100    100       100      std two
3            Irene  80     70        70       std two

我试图输出AVG,每个学生合格的总和,但是我没有把所有课程的结果都输出。

示例:现在搜索时结果是这样的。

student_id | Name | Math | English | Sience | Class    Average Total
1            John   100    90        89       std two  93      279

当我搜索另一名学生时,我得到了所需的答案,但是当我搜索“std two”作为班级名称时,我得到的第一名学生的结果是1名student_id而不是全班。 我想要的答案是这样或任何格式,但应输出所有类的结果。

student_id | Name | Math | English | Sience | Class    Average Total
1            John   100    90        89       std two  93      279 

student_id | Name | Math | English | Sience | Class    Average Total
2            Simon  100    100       100      std two  100     300

 student_id | Name | Math | English | Sience | Class    Average Total
 3            Irene  80     70        70       std two  73.3    220

这是我的php代码

<?php


 //include mysql connect
   $query='query';

   if (isset($_GET['query'])) 
{    
   $query=$_GET['query'];


      } 

    $min_length = 2;
   // you can set minimum length of the query if you want

    if(strlen($query) >= $min_length){ // if query length is more or equal minimum   
  length  
   then


    $query = htmlspecialchars($query); 

    // changes characters used in html to their equivalents, for example: < to &gt;

    $query = mysql_real_escape_string($query);
    // makes sure nobody uses SQL injection



    $raw_results = mysql_query("SELECT *, AVG(math+english+science)/3 as  
  Average, (math+english+science)as Total from table1
     WHERE (`name` LIKE '%".$query."%') or (`class` LIKE '%".$query."%')") or  
    die(mysql_error()); 

     // '%$query%' is what we're looking for, % means anything, for example if $query 
    is Hello
    // it will match "hello", "Hello man", "gogohello", if you want exact match use   
  `title`='$query'
    // or if you want to match just full word so "gogohello" is out use '% $query %'  
  ...OR ... '$query %' ... OR ... '% $query'
    if(strlen($query) >= $min_length){
      // if query length is more or equal minimum length the

    if(mysql_num_rows($raw_results) >= 0){
     // if one or more rows are returned do following


         // $results = mysql_fetch_array($raw_results) puts data from database into   
    array, while it's valid it does the loop

                  while($results = mysql_fetch_array($raw_results)){

//The echo table         


 echo "<table width='500' height='2' cellpadding='2' cellspacing='0' border='0'>";
 echo"<tr><td>Id_number</td><td>Name</td><td>Math</td><td>English</td><td>Science</td>     
<td>Class</td><td>Average</td><td>Total</td>";
echo "<tr>"."<td>".$results["student_id"]."</td>"."<td>".$results["name"]."</td>"."   
<td>".$results["math"]."</td>"."<td>".$results["english"]."</td>"."  
<td>".$results["science"]."<td>".$results["class"]."<td>".$results["Average"]."</td>"." 
<td>".$results["Total"]."</td>"."</p>";
echo"</table>";  




        } 

    }   
             }


        }





    ?>

1 个答案:

答案 0 :(得分:1)

更改

AVG(math+english+science)/3

ROUND((math+english+science)/3)

AVG是一个聚合函数,它会在您使用GROUP BY时平均所选行的所有行的值或组中所有行的值。如果您只想要同一行中3列的平均值,只需添加它们并除以列数。