I've a db where I store the answers given by users (table 'risposta_utente') to questions belonging to different lessons (table 'lezioni'). I want to get the lesson with the maximum average of the votes obtained by an user answering its questions.
ID_corso_sede_utente uniquely identify an user following the lessons of one course (because there are more then one course).
SELECT titolo, MAX(voto) as voto_max
FROM (
SELECT AVG(voto_grezzo) as voto, ID_lezione
FROM risposta_utente
WHERE ID_corso_sede_utente = 260, risposta_utente.attivo = 1
GROUP BY ID_lezione
) AS voti
JOIN lezioni
ON lezioni.ID_lezione=voti.ID_lezione
Mysql signal errors on lines 2, 3 and 7 (around the brackets enclosing the subquery). In particular it say:
- expecting expression, "(" found (line 2);
- unexpected token near "(" (line 3);
- unexpected token near ")" (line 7);
- unknown keyword near "AS" (line 7);
- unexpected token near "voti" (line 7);
WHERE ID_corso_sede_utente = 260, risposta_utente.attivo = 1should probably beWHERE ID_corso_sede_utente = 260 AND risposta_utente.attivo = 1