SQL: Join Parent - Child tables

Posted by pray4Mojo on Stack Overflow See other posts from Stack Overflow or by pray4Mojo
Published on 2012-07-09T20:46:28Z Indexed on 2012/07/09 21:15 UTC
Read the original article Hit count: 216

Filed under:
|
|
|

I'm building a simple review website application and need some help with SQL Query.

There are 3 tables (Topics, Comments, Users). I need a SQL query to select the data from all 3 tables.

The 'Topics' table is the parent and the 'Comments' table contains the child records (anywhere from zero to 100 records per parent.

The third table 'Users' contains the user information for all users.

Here are the fields for the 3 tables:

Topics (topicID, strTopic, userID)
Comments (commentID, topicID, strComment, userID)
Users (userID, userName)

I tried:

SELECT * 
FROM   Topics 
  Inner Join Comments ON Topics.topicID = Comments.topicID
  Inner Join Users ON Topics.userID = Users.userID

But this does not work correctly because there are multiple topics and the User info is not joined to the Comments table. Any help would be appreciated.

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server