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


i want data 2 table based on userid=user1 parent , child table shown in below image enter image description here

i think should work:

select      parent.commentid commentid,             parent.userid userid,              parent.message message,             parent.date date,             null parentcommentid        #parentcomment parent       userid = 'user1'  union  select      child.c_commentid commentid,             child.c_userid userid,              child.c_message message,             child.c_date date,             child.c_parentcommentid parentcommentid        #childcomment child       child.c_userid = 'user1' 

here's sql fiddle : http://sqlfiddle.com/#!9/3bb46/2

hope helps!!!


Comments