sql server 2008 - How do I join tables on multiple conditions -
i have select data 2 tables using join both working individual not working together, when execute complete query not work when execute both joins individually example:
select dbo.mail_messages.subject, dbo.mail_messages.body, dbo.mail_messages.date, dbo.mail_messages.messageid --dbo.mail_messages.parrentid dbo.mail_messages join dbo.mail_reply mr1 on mr1.messageid = dbo.mail_messages.messageid
====================================================
select dbo.mail_messages.subject, dbo.mail_messages.body, dbo.mail_messages.date, dbo.mail_messages.messageid dbo.mail_messages join dbo.mail_reply mr2 on mr2.parrentid = dbo.mail_messages.messageid
above queries work fine.
but when execute below query (combination of above queries) returns nothing.
select dbo.mail_messages.subject, dbo.mail_messages.body, dbo.mail_messages.date, dbo.mail_messages.messageid --dbo.mail_messages.parrentid dbo.mail_messages join dbo.mail_reply mr1 on mr1.messageid = dbo.mail_messages.messageid join dbo.mail_reply mr2 on mr2.parrentid = dbo.mail_messages.messageid
i have attached image:
i think need filter according message id or parentid , return status of message
select distinct t1.* mail_reply t2 join mail_messages t1 on (t2.[message id]=t1.[message id] or t2.parentid=t1.[message id])
now if want filter message id or parentid append code below code
and (t2.[message id]=2 or t2.parentid=2)
now if want filter message id or parentid , userid append code below code
and (t2.[message id]=2 or t2.parentid=2) , t2.userid=1
Comments
Post a Comment