Tuesday, October 30, 2007

SQL Statements within SQL Statements

I ran into this one while working with discussions. I wanted to show a summary for the discussion and for some reason was stuck when trying to bring back a description and then show the number of replies. Here's how it worked.

SELECT
d.*,
u.FirstName ,
u.LastName ,
(Select Count(*) as ResponseCount from DiscussionThreads dt where dt.DiscussionID=d.DiscussionID) AS ResponseCount
FROM
Discussions d,
Users u
WHERE
u.UserID=d.UserID

No comments: