[英]SQL query on the same table with join and group by
I've a problem with this query: 我的查询有问题:
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
but it always respond me: 但它总是回应我:
You have an error in your SQL syntax;
您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'JOIN db_dati parent ON source.stagione=parent.stagione AND source.cod=parent.cod' at line 1
查看与您的MySQL服务器版本对应的手册,以便在第1行'JOIN db_dati parent ON source.stagione = parent.stagione AND source.cod = parent.cod'附近使用正确的语法
You need to join tables before WHERE
clause: 您需要在
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
There's some redundant stuff in there... 那里有一些多余的东西......
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.