简体   繁体   中英

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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