有关PHP代码的任何想法吗?

时间:2013-09-25 13:11:16

标签: php mysql math if-statement sum

我需要做一些数学计算,而我似乎无法找到办法。

我有以下表格,来自此查询:

SELECT materii.id,
       materii.materie,
        GROUP_CONCAT(note.note) AS note,
        GROUP_CONCAT(DISTINCT teza.`teza`) AS teza


FROM materii
LEFT JOIN teza ON materii.id = teza.id_materie
LEFT JOIN note ON materii.id = note.id_materie
LEFT JOIN elevi ON note.id_elev = elevi.cod_elev 
LEFT JOIN luni ON note.`luna_nota`=luni.`id`
WHERE elevi.`cod_elev` = 1 AND luna_nota = 9
GROUP BY materii.id, materii.materie
ORDER BY materii.materie

table

我需要做类似的事情:

$notele = mysql_query($pentrumedie)
                or die("Nu am gasit note in baza de date");
$numar_note = mysql_num_rows($notele);
if($numar_note==0)
{

}
else 
{
    while($rand2=mysql_fetch_array($notele))
    {     
         $note1 = ($rand2['notele'] / $numar_note);
       $medie_septembrie = ($note1 / $cate_note_sunt);
    }
}

我需要为所有“注释”执行类似的操作,但是$ rand2 ['notele']需要是它们的总和,如果有更多“注释”,并且如果有“teza”的值“,那么数学公式必须是:

(($rand2['notele'] / $numar_note) * 3 + teza) / 4

我尝试了一些功能,但没有一个工作....任何想法?感谢!!!

1 个答案:

答案 0 :(得分:2)

您可以尝试更改一下您的sql语句

SELECT materii.id,
   materii.materie,
    SUM(note.note)/COUNT(note.note) AS medie,
    GROUP_CONCAT(DISTINCT teza.`teza`) AS teza
FROM materii
LEFT JOIN teza ON materii.id = teza.id_materie
LEFT JOIN note ON materii.id = note.id_materie
LEFT JOIN elevi ON note.id_elev = elevi.cod_elev 
LEFT JOIN luni ON note.`luna_nota`=luni.`id`
WHERE elevi.`cod_elev` = 1 AND luna_nota = 9
GROUP BY materii.id, materii.materie
ORDER BY materii.materie

在php中使用

之类的东西
$medii = array();
while($rand = mysql_fetch_assoc($notele))
{
  if($rand["teza"] == "" || $rand["teza"] == NULL)
  {
   $medii[] = array("materie" => $rand["materie"],
                  "medie" => $rand["medie"]);
  }
  else
  {
   $medii[] = array("materie" => $rand["materie"],
                  "medie" => ((float)$rand["medie"]*3 + (float)$rand["teza"])/4);
   }
}
var_dump($medii);