show records that has matching UserAccountID and ParentUserAccountID and non matching records using self join?

Umair Mubeen :

I want to fetch the data from database whose UserAccountID is match with ParentUserAccount ID

Here is the Sql Query

SELECT Current.Status, 
       Current.UserID as UserID, 
       Current.UserAccountID as C_UserAccID,
       Current.Implementation_Type as C_ImpType,
       Current.ClientID as C_ClientID,
       tbl_clients.ClientName,
       parent.LastModifiedDate as PLastModifiedDate,
       parent.ParentUserAccountID as PUserAccID,
       parent.Implementation_Type as PImpType,
       parent.IterationNum as PIterationNum,
       parent.Description as PDescription,
       parent.GSheetID  as PGSheetID 
FROM tbl_useraccounts AS Current 
JOIN tbl_useraccounts AS parent on parent.ParentUserAccountID=Current.UserAccountID   
INNER JOIN tbl_clients on Current.ClientID = tbl_clients.ClientID 
WHERE  Current.UserID ='7' 
  and Current.Status!='2' 
  and parent.ParentUserAccountID!='' 
  and parent.Implementation_Type='4' 
order by  parent.LastModifiedDate desc

but i also want to show data in database whose C_UserAccID not equal to PUserAccountID

Any Help would be Appreciated thanks in Advance

Koteswara Rao :

SELECT Current.Status,

   Current.UserID as UserID, 

   Current.UserAccountID as C_UserAccID,

   Current.Implementation_Type as C_ImpType,

   Current.ClientID as C_ClientID,

   tbl_clients.ClientName,

   parent.LastModifiedDate as PLastModifiedDate,

   parent.ParentUserAccountID as PUserAccID,

   parent.Implementation_Type as PImpType,

   parent.IterationNum as PIterationNum,

   parent.Description as PDescription,

   parent.GSheetID  as PGSheetID 

FROM tbl_useraccounts AS Current

LEFT JOIN tbl_useraccounts AS parent on

parent.ParentUserAccountID=Current.UserAccountID

INNER JOIN tbl_clients on Current.ClientID =

tbl_clients.ClientID

WHERE Current.UserID ='7'

and Current.Status!='2'

and parent.ParentUserAccountID!=''

and parent.Implementation_Type='4'

order by parent.LastModifiedDate descv

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=371664&siteId=1