Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Count rows with join

Author  Topic 

axell14
Starting Member

3 Posts

Posted - 2014-06-21 : 18:13:58
Hi!

Well, I'm so stuck that I have to ask for help with this. I've searched high and low and found no solution.

This is my sql string. It counts all the rows in Questions table but it should only count the rows where id in Quizzes matches the quiz column in Questions table.

"select Quizzes.name, Quizzes.id, count(Questions.quiz) as total from Quizzes inner join Questions on Quizzes.id=Questions.quiz"

Why isnt it doing what I want it to do?

bitsmed
Aged Yak Warrior

545 Posts

Posted - 2014-06-21 : 18:28:54
You probably want to append "group by Quizzes.name,Quizzes.id" to the end of your query.
Go to Top of Page

axell14
Starting Member

3 Posts

Posted - 2014-06-22 : 05:38:31
Yes, that did it. Thank you.

Any one care to explain what the grouping does here.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2014-06-23 : 06:26:56
When you add Quizzes.name, Quizzes.id in the GROUP BY Clause, the counting is based on those two columns. Otherwise MySQL counts everything and display random Quizzes.name, Quizzes.id in the SELECT statement

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

axell14
Starting Member

3 Posts

Posted - 2014-06-24 : 04:54:32
Ok, I see. Thanx for elaborating on that.
Go to Top of Page
   

- Advertisement -