sql >> Base de Datos >  >> RDS >> Mysql

No puedo entender cómo obtener datos de MySql Query

Tiene algunos problemas:

  1. Los índices de $row debe estar entre comillas, p. $row['name'] de lo contrario, recibirá muchas advertencias de "Constante indefinida";
  2. $row['Jan'] es la forma correcta de recuperar los datos mensuales, necesita replicar eso para cada uno de los otros meses;
  3. reemplazar $avgGrade con $row['AVG']
  4. No estoy seguro de lo que intenta lograr con
<td class = "gScore-<?php echo $row[Jan]; ?>"></td>

¿de verdad quieres?

<td class = "gScore"><?php echo $row[Jan]; ?></td>
  1. necesitas agregar el -- repeat for May to November filas a la consulta, es decir,
    $result = $db->query("SELECT name,
       COALESCE(AVG(CASE WHEN mth = 1 THEN PTS END), 0) AS Jan,
       COALESCE(AVG(CASE WHEN mth = 2 THEN PTS END), 0) AS Feb,
       COALESCE(AVG(CASE WHEN mth = 3 THEN PTS END), 0) AS Mar,
       COALESCE(AVG(CASE WHEN mth = 4 THEN PTS END), 0) AS Apr,
       COALESCE(AVG(CASE WHEN mth = 5 THEN PTS END), 0) AS May,
       COALESCE(AVG(CASE WHEN mth = 6 THEN PTS END), 0) AS Jun,
       COALESCE(AVG(CASE WHEN mth = 7 THEN PTS END), 0) AS Jul,
       COALESCE(AVG(CASE WHEN mth = 8 THEN PTS END), 0) AS Aug,
       COALESCE(AVG(CASE WHEN mth = 9 THEN PTS END), 0) AS Sep,
       COALESCE(AVG(CASE WHEN mth = 10 THEN PTS END), 0) AS Oct,
       COALESCE(AVG(CASE WHEN mth = 11 THEN PTS END), 0) AS Nov,
       COALESCE(AVG(CASE WHEN mth = 12 THEN PTS END), 0) AS `Dec`,
       AVG(PTS) AS AVG
   FROM (
   SELECT name, PTS AS PTS, MONTH(STR_TO_DATE(DATE, '%a %b %e %H:%i:%s %Y')) AS mth
   FROM data
    ) d
    GROUP BY name");