简体   繁体   English

两次求和结果 mysql

[英]Sum result of two consults mysql

I have this query:我有这个查询:

SELECT nombrelocal, COUNT(*) FROM `resultados` WHERE id_liga = '1235' AND (reslocal + resvisitante) >= 2 AND reslocal != 99 AND resvisitante != 99 GROUP BY nombrelocal

Result:结果:

nombrelocal         COUNT(*)    
Alaves              10
Athletic             8
Atletico de Madrid   7

And I have this other query:我还有另一个查询:

SELECT nombrevisitante, COUNT(*) FROM `resultados` WHERE id_liga = '1235' AND (reslocal + resvisitante) >= 2 AND reslocal != 99 AND resvisitante != 99 GROUP BY nombrevisitante

Result:结果:

nombrelocal         COUNT(*)    
Alaves              7
Athletic            5
Atletico de Madrid  3

I would like sum the two querys, I want this result:我想总结两个查询,我想要这个结果:

nombrelocal         COUNT(*)    
Alaves              17
Athletic            13
Atletico de Madrid  10

Thank you谢谢

The safest approach is probably UNION ALL :最安全的方法可能是UNION ALL

SELECT nombre, COUNT(*)
FROM (
    SELECT nombrelocal nombre
    FROM `resultados` 
    WHERE id_liga = '1235' AND (reslocal + resvisitante) >= 2 AND reslocal != 99 AND resvisitante != 99 
    UNION ALL
    SELECT nombrevisitante nombre
    FROM `resultados` 
    WHERE id_liga = '1235' AND (reslocal + resvisitante) >= 2 AND reslocal != 99 AND resvisitante != 99 
) t
GROUP BY nombre

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

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