【问题标题】:show records that has matching UserAccountID and ParentUserAccountID and non matching records using self join?使用自联接显示具有匹配 UserAccountID 和 ParentUserAccountID 的记录和不匹配的记录?
【发布时间】:2020-03-26 10:33:26
【问题描述】:

我想从 UserAccountID 与 ParentUserAccount ID 匹配的数据库中获取数据

这里是 Sql 查询

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

但我也想在 C_UserAccID 不等于 PUserAccountID 的数据库中显示数据

任何帮助都将不胜感激

【问题讨论】:

  • 对不起,我自己的查询出错了,现在问题已经解决了

标签: mysql sql join self-join


【解决方案1】:
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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多