user6503334 user6503334 - 1 year ago 84
SQL Question

How to get Parent and Child Table data as a single query in Sql

I want to get all data from 2 table based on


from parent and child table as i shown in below image
enter image description here

Answer Source

I think this should work:

SELECT      parent.CommentID as CommentID,
            parent.userID as userID, 
            parent.Message as Message,
            parent.Date as date,
            NULL as ParentCommentID
FROM        #ParentComment parent
WHERE       UserID = 'user1'


SELECT      child.c_commentID as CommentID,
            child.c_userID as userID, 
            child.c_message as Message,
            child.c_Date as date,
            child.c_parentcommentID as ParentCommentID
FROM        #ChildComment child
WHERE       child.c_userID = 'user1'

Here's an SQL fiddle :!9/3bb46/2

Hope this helps!!!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download