[英]Sum result of two consults mysql
我有這個查詢:
SELECT nombrelocal, COUNT(*) FROM `resultados` WHERE id_liga = '1235' AND (reslocal + resvisitante) >= 2 AND reslocal != 99 AND resvisitante != 99 GROUP BY nombrelocal
結果:
nombrelocal COUNT(*)
Alaves 10
Athletic 8
Atletico de Madrid 7
我還有另一個查詢:
SELECT nombrevisitante, COUNT(*) FROM `resultados` WHERE id_liga = '1235' AND (reslocal + resvisitante) >= 2 AND reslocal != 99 AND resvisitante != 99 GROUP BY nombrevisitante
結果:
nombrelocal COUNT(*)
Alaves 7
Athletic 5
Atletico de Madrid 3
我想總結兩個查詢,我想要這個結果:
nombrelocal COUNT(*)
Alaves 17
Athletic 13
Atletico de Madrid 10
謝謝
最安全的方法可能是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.