Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

count unique combinations in MySQL

I want to count number of matches between two teams.

select home.home_team, home.away_team, (home_matches_against+away_matches_against) as matches
from
(select home_team, away_team, count(home_team) as home_matches_against
from results
group by home_team, away_team) as home
join
(select away_team, home_team, count(away_team) as away_matches_against
from results
group by away_team, home_team) as away
on home.home_team=away.away_team and home.away_team=away.home_team

output:
enter image description here

As you can see count of matches between tow teams are duplicated.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My desired result is for example:

home team  away _team  matches
England    Scotland    117
Wales      Scotland    106 

and etc. I dont want count of matches to be duplicated

>Solution :

You could add WHERE clause:

WHERE home.home_team < home.away_team;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading