I’m using three tables. In the first table are recorded customer questions.
customer_id | question | question_id
----------------------------------------------
58 | question 4 | 4
41 | question 5 | 5
37 | question 3 | 3
In the second table are recorded comments relevant to questions from the first teble
comment | question_id | user
---------------------------------------
comment 1 | 4 | 41
comment 2 | 5 | 58
comment 3 | 4 | 41
comment 4 | 5 | 58
comment 5 | 3 | 23
In the third table are located data about the users of this site
user | status
--------------------------------
58 | 1
41 | 1
37 | 0
23 | 0
How can I make a query that will result as a list of the last five questions sorted by question_id and the total number of comments related to each question. These questions and comments can be only by the users with the status “1” a third table.
The result in this example should look like:
question | total comments | user
-----------------------------------------------
question 5 | 2 | 41
question 4 | 2 | 58
I’m reasonably certain this is correct, though I don’t have the data here to validate.
The LEFT JOIN on questions will allow questions to be represented even if no comments have been left on them.
Let me know if you have any questions.