简体   繁体   English

子句中的mysql子查询给出语法错误

[英]Mysql subquery in from clause give syntax errors

I've a db where I store the answers given by users (table 'risposta_utente') to questions belonging to different lessons (table 'lezioni'). 我有一个数据库,用于存储用户(表“ risposta_utente”)对属于不同课程(表“ 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). ID_corso_sede_utente会根据一门课程的课程唯一地标识用户(因为有多门课程)。

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). Mysql在第2、3和7行(包围子查询的方括号内)发出错误信号。 In particular it say: 具体说:

  • expecting expression, "(" found (line 2); 期望表达式“(”找到(第2行);
  • unexpected token near "(" (line 3); “(”附近的意外令牌(第3行);
  • unexpected token near ")" (line 7); “)”附近出现意外令牌(第7行);
  • unknown keyword near "AS" (line 7); “ AS”附近的未知关键字(第7行);
  • unexpected token near "voti" (line 7); “ voti”附近出现意外令牌(第7行);

'Where' clause should be separated by "and" and not comma. “ Where”子句应以“和”而不是逗号分隔。

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 **and** risposta_utente.attivo = 1
    GROUP BY ID_lezione
) AS voti 
JOIN lezioni
ON lezioni.ID_lezione=voti.ID_lezione

And one more thing, if you are running this is unix mysql console then adding tab will throw an error. 还有一件事,如果您正在运行的是unix mysql控制台,则添加选项卡将引发错误。 Remove the blank spaces before each line and try 删除每行之前的空格,然后尝试

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 **and** risposta_utente.attivo = 1
GROUP BY ID_lezione
) AS voti 
JOIN lezioni
ON lezioni.ID_lezione=voti.ID_lezione

Or try executing it as a single line. 或者尝试将其作为一行执行。

where command should be as expression. where命令应尽可能表达。

either 要么

WHERE ID_corso_sede_utente = 260 AND risposta_utente.attivo = 1

or 要么

WHERE ID_corso_sede_utente = 260 OR risposta_utente.attivo = 1

based on your business logic 根据您的业务逻辑

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM