Home > SQL

Query for getting the childtable no of rows related to one single row in parent table

28. February 2013
SELECT TOP 5 temp.totalcomments,NewsLetter.title,NewsLetter.nID 
FROM
(SELECT NewsLetter.nID,NewsLetter.title ,COUNT(a.nID) AS TotalComments
FROM NewsLetter LEFT OUTER JOIN (select nID from Comments where approved='true') as a
ON a.nID=NewsLetter.nID
GROUP BY NewsLetter.nID,NewsLetter.title) AS temp,Newsletter
WHERE scID=10 AND temp.nID=NewsLetter.nID AND trash='false'
ORDER BY totalcomments DESC;

SQL

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading