0

我需要做一些数学运算,但我似乎找不到办法。

我有下表,由此查询产生:

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

桌子

我需要做类似的事情:

$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);
    }
}

我需要为所有“note”做类似的事情,但如果有更多“note”,并且“teza”有值,则 $rand2['notele'] 需要是它们的总和,然后数学公式必须是:

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

我尝试了一些 if 函数,但它们都不起作用....有什么想法吗?谢谢!!!

4

1 回答 1

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);
于 2013-09-25T13:32:52.413 回答