开发者

Help me to create a join based query in SQL Server 2008

The Join I have implemented is a basic one, but I can't figure out the issue in my query. Can you help me to figure out where I have made a mistake?

Here is the scenario:

I have the following tables

Help me to create a join based query in SQL Server 2008

and I am writing this query in T-SQL

Select 
   f._id, f.createdby, f.fullname, 
   f.topictitle, f.topicdate, f.status,
   f.totalviews, count(fr._id) as totalResponses 
from     
forumresponse as fr
RIGHT OUTER Join forum as f ON f._id = fr.forumId
where f.categoryId= @categoryId  
group by f._id, f.createdby, f.fullname, f.topictitle, 
         f.topicdate, f.status, f.totalviews
order by _id desc 

But everytime I am getting same list of forums for any category.

I am trying to fetch the forum and its details which belongs to a particualr cate开发者_Go百科gory only. But whatever the category I am passing I'm getting same list of forum.


Don't use a RIGHT OUTER JOIN - use a regular INNER JOIN.

Using the outer join ensures all records from the right table (forum) will be returned, whether there is a matching one from the left table or not.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜