Jump to content

Really can't figure this one out


morphboy23

Recommended Posts

Okay, so I've been trying for a while to figure this out, and haven't found a solution yet. 

 

The example is thus:  there is a 'questions' table, with a column 'idquestion' (among others that don't matter right now).  there is also an 'answers' table, which also has a 'idquestion' column which refers to the questions table one.  multiple rows in 'answers' might refer to the same idquestion.

 

I need to figure out a query that will select every questions.idquestion and the COUNT() of how many rows in 'answers' has that idquestion. 

So, for example,  if 3 answers point to idquestion=1, 2 answers point to idquestion=2, and there are 0 rows that point to idquestions 3 through 10, I want:

 

idquestion / COUNT()

1            /      3

2            /      2

3            /      0

...            /    ...

10          /      0

 

Thanks for any help

Link to comment
Share on other sites

Hm after some tinkering I think I got it..

 

SELECT questions.idquestion AS id, (

SELECT COUNT( answers.idinquiry )
FROM answer
WHERE answer.idquestion = id
) AS counter
FROM question
GROUP BY question.idquestion
ORDER BY counter DESC

 

It works, but seems long..but maybe not? If anyone knows an easier/shorter way, please share!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.