0

I am trying to find out how many comments there are in a question that has been made by the owner of the page (the admin).

tblQuestion: (question table)

queID
queUserID
queCompanyID (the owner of the page where questions are asked)

tblReplies: (where comments in the question are saved)

repQuestionID (queID = parent ID)
repUserID (the user ID who made the comment)

tblUsers: (where the users are stored)

uID (user ID, autoincrement)
uCompanyID (if this id is the same as a company id, the user is admin of that page)

So, I want to know how many reply posts are made by the company owner (a user with uCompanyID the reply posts' parent companyID - queCompanyID).

I tried doing this to get number of posts made by page admin, but does not seem to work:

SELECT COUNT(*) tblReplies.repID
FROM tblReplies
JOIN 
    tblQuestions ON tblQuestions.queID = tblReplies.repQuestionID
JOIN 
    tblUsers ON tblQuestions.queCompanyID = tblUsers.uCompanyID
WHERE tblQuestions.queID = 68 AND tblUsers.uCompanyID = 1

I really hope there is a ninja out there who can help me, I've spent hours and still nothing.

Thanks!

1
  • Your schema description doesn't seem to be complete. For instance, your tblUsers only has one field and you make no reference to Primary Keys or Foreign Keys.
    – Aheho
    Jan 22, 2013 at 2:51

3 Answers 3

1

This should work (assuming there is a userID on the tblUsers table.:

SELECT 
    count(*), 
    tblReplies.repID
FROM 
    tblReplies, 
    tblQuestions, 
    tblUsers
WHERE
    tblQuestions.queID = tblReplies.repQuestionID AND
    tblQuestions.queCompanyID = tblUsers.uCompanyID AND
    tblUser.uID = tblReplies.repUserID AND
    tblUsers.uCompanyID = 1 AND 
    tblQuestions.queID = 68 

GROUP BY tblReplies.repID
1
  • Thank you very much for your help, I tried this solution but I receive following error: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'tblUsers.uCompanyID = 1 AND tblQuestions.queID = 68 GROUP BY tblReplies.r' at line 12. Also, there is a userID called uID as you guessed.
    – David
    Jan 22, 2013 at 3:30
0
SELECT COUNT(*) AS total 
FROM tblReplies 
WHERE repQuestionID =68 AND repUserID =1
0

This should work.

SELECT  count(*) as num_of_comments
FROM tblQuestion a
INNER JOIN tblReplies  b
    on a.queID = b.repQuestionID
INNER JOIN tblUsers c
    on b.queCompanyID = c.uCompanyID 
where a.queID = 68
    and c.uCompanyID = 1
2
  • This solution gave all posts that were added under the parent ID. Here is the code I used: SELECT COUNT( * ) AS num_of_comments FROM tblQuestions INNER JOIN tblReplies ON tblQuestions.queID = tblReplies.repQuestionID INNER JOIN tblUsers ON tblReplies.repCompanyID = tblUsers.uCompanyID WHERE tblQuestions.queID =68 AND tblUsers.uCompanyID =1
    – David
    Jan 22, 2013 at 3:32
  • Thank you! This works like a charm, I'm truly grateful for your terrific help.
    – David
    Jan 22, 2013 at 4:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.