I have to say I’m an amateur on MySQL, plus english is not my mother’s language.
Here is my first table “Teacher”:
ID Name Procedence 1 John Italy 2 Mike Russia 3 Lauren Spain 4 Arnold Spain
And here my second table “Course”:
ID1 CourseName 1 Sailing 1 Football 2 Basketball 2 Hockey
I would like to know which country has more teachers giving courses, but I don’t want that it count the same teacher twice. So far I made this:
SELECT Procedence, Count(Procedence) as Procedence_Count from Teacher INNER JOIN Course ON Course.ID1 = Teacher.ID GROUP by Procedence having Procedence_Count > 1;
When I run this, I obtain:
Procedence Procedence_Count Italy 2 Russia 2
But the code counts John and Mike as two persons, I would like to remove that duplicity, so I would like to obtain:
Procedence Procedence_Count Italy 1 Russia 1
Thanks a lot.
Advertisement
Answer
Use count(distinct)
:
select Procedence, Count(distinct t.id) as teacher_count from Teacher t join Course c on c.ID1 = t.ID group by Procedence having teacher_count > 1;
One additional note: “procedence” is not at all a common English word. One word is “provenance”. I think more commonly “origin” or just “country” would be used.