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
https://forums.phpfreaks.com/topic/57210-really-cant-figure-this-one-out/
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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.