We help IT Professionals succeed at work.

group by count and join involving three tables

VirusMinus
VirusMinus asked
on
801 Views
Last Modified: 2008-01-09
for a quiz database i have three tables:

------------
questions
------------
q_id      q_title                topic_name      member_id
1         why is?                 Math                     17
2         what for?               Physics               20
3         How about?            Math                  18
4         and this?               Biology                 20
5         also by?                Physics               18


-----------
answers
-----------
a_id   answer_text      q_id      
1       it is!                      1
2       not bad                 2
3       for me                  4
4       you are                  1
5       likely to                  3


------------
members
------------
member_id      member_name
17                        Dave            
20                        Simon            
18                        Alice      

I would like to pull all the question info, member's name who asked the question and the number of answers that each question received, for a certain topic name.

i was able to do this so far:

eg. for Physics

select questions.q_id, questions.q_title, questions.topic_name,  count(answers.q_id) as replies from questions LEFT JOIN  answers ON questions.q_id = answers.q_id
where questions.topic_name ='Physics'
GROUP BY questions.q_title, questions.q_id, questions.topic_name

which brings the correct information, except for the member's name who asked the question.

q_id      title            topic_name      replies
2      what for?      Physics               1
5      also by?       Physics                0

I've tried but can't figure out how to get the member's name into the query.

also is there any other way.. all the info that i pull, i have to also include it in the GROUP BY. i plan to have more in the questions table. if i want to get that, will i have to include that in the GROUP BY as well? and does it have to be in the same order?

and any improvements, optimizations to the query are welcome.

Thanks :)
 
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Top Expert 2007

Author

Commented:
hi capricorn1,

i had to remove the extra brackets around the WHERE and it worked.. were they for any specific reason?

also from your query it looks like any more info that i would need to get from the questions table would also be need to be added to the GROUP BY. does the order matter?

Thanks..

CERTIFIED EXPERT
Top Expert 2016

Commented:
<i had to remove the extra brackets around the WHERE and it worked.. were they for any specific reason?>

yeah there was an extra bracket.

<also from your query it looks like any more info that i would need to get from the questions table would also be need to be added to the GROUP BY. does the order matter?>

you have to add them to the Group by if you will not use the aggragate functions like count, sum etc..
or you will have an arror message when you run the query
Top Expert 2007

Author

Commented:
cool. thanks again :)
CERTIFIED EXPERT
Top Expert 2016

Commented:
U R Welcome!!!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.