多重连接 Mysql 将 SUM 值加倍
我试图进行查询,但它使总和值加倍
SELECT
cidades.id AS id,
cidades.name AS municipio,
Sum(conjuntos.n_uhs) AS uh,
programas.id AS programa,
conjuntos.name
FROM
conjuntos
Inner Join conjuntos_programas ON conjuntos_programas.conjunto_id = conjuntos.id
Inner Join programas ON programas.id = conjuntos_programas.programa_id
Inner Join cidades ON conjuntos.cidade_id = cidades.id
WHERE
conjuntos.situation_id = 2
GROUP BY
conjuntos.cidade_id
ORDER BY
municipio ASC
Im trying to make a a query, but its doubling the Sum values
SELECT
cidades.id AS id,
cidades.name AS municipio,
Sum(conjuntos.n_uhs) AS uh,
programas.id AS programa,
conjuntos.name
FROM
conjuntos
Inner Join conjuntos_programas ON conjuntos_programas.conjunto_id = conjuntos.id
Inner Join programas ON programas.id = conjuntos_programas.programa_id
Inner Join cidades ON conjuntos.cidade_id = cidades.id
WHERE
conjuntos.situation_id = 2
GROUP BY
conjuntos.cidade_id
ORDER BY
municipio ASC
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有重复的行,您可以通过从查询中删除
group by
和SUM(...
来检查这一点。按如下方式更改查询并告诉我这是否可以解决问题。
You've got duplicate rows, you can check this by removing the
group by
and theSUM(...
from your query.Change the query as follows and tell me if that fixes to problem.
听起来您的两个或多个表之间存在一对多关系来执行此操作。尝试执行
SELECT *
并开始调试查询以查看重复行的位置。It sounds like you have a one to many relationship between two or more of your tables to be doing this. Try doing a
SELECT *
and start debugging your query to see where it is duplicating the rows.