0

我对这个查询有疑问:

SELECT source.cod,source.nome,source.ruolo,SUM(source.giocato),ROUND(SUM(source.fvoto)/SUM(source.giocato),2) as media,ROUND(SUM(source.voto)/SUM(source.giocato),2),SUM(source.gs),SUM(source.ass),SUM(source.amm),SUM(source.esp),SUM(source.rigsub),SUM(source.rigpar),source.stagione,parent.stagione,parent.cod,parent.squadra,parent.valore,parent.esiste,parent.gg
FROM db_dati source
WHERE source.ruolo=".$q." AND source.stagione=".$stagione."
GROUP BY source.cod,source.politico,source.stagione
HAVING source.politico=0
JOIN db_dati parent
ON source.stagione=parent.stagione AND source.cod=parent.cod
ORDER BY media DESC

但它总是回应我:

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“JOIN db_dati parent ON source.stagione=parent.stagione AND source.cod=parent.cod”附近使用正确的语法

4

2 回答 2

2

WHERE您需要在子句之前加入表:

SELECT source.cod,source.nome,source.ruolo,SUM(source.giocato),ROUND(SUM(source.fvoto)/SUM(source.giocato),2) as media,ROUND(SUM(source.voto)/SUM(source.giocato),2),SUM(source.gs),SUM(source.ass),SUM(source.amm),SUM(source.esp),SUM(source.rigsub),SUM(source.rigpar),source.stagione,parent.stagione,parent.cod,parent.squadra,parent.valore,parent.esiste,parent.gg
FROM db_dati source
JOIN db_dati parent
ON source.stagione=parent.stagione AND source.cod=parent.cod
WHERE source.ruolo=".$q." AND source.stagione=".$stagione."
GROUP BY source.cod,source.politico,source.stagione
HAVING source.politico=0
ORDER BY media DESC
于 2013-07-13T10:10:39.320 回答
0

里面有些多余的东西...

 SELECT source.cod
      , source.nome
      , source.ruolo
      , SUM(source.giocato)
      , ROUND(SUM(source.fvoto)/SUM(source.giocato),2) media
      , ROUND(SUM(source.voto)/SUM(source.giocato),2)
      , SUM(source.gs)
      , SUM(source.ass)
      , SUM(source.amm)
      , SUM(source.esp)
      , SUM(source.rigsub)
      , SUM(source.rigpar)
      , source.stagione
      , parent.stagione
      , parent.cod
      , parent.squadra
      , parent.valore
      , parent.esiste
      , parent.gg
   FROM db_dati source
   JOIN db_dati parent
     ON parent.stagione = source.stagione 
    AND parent.cod = source.cod
  WHERE source.ruolo = ".$q." 
    AND source.stagione=".$stagione."
    AND source.politico=0
  GROUP 
     BY source.cod
  ORDER BY media DESC
于 2013-07-13T10:49:33.880 回答