SQL表的自身关联
SQL表的自身关联
有如下两个数据表:
tprt表,组合基本信息表,每个组合有对应的投管人和托管人;
tmanager表,管理人信息表,管理人类别由o_type区分;
具体表信息如下所示:
tprt表:
tmanager表:
现需查询出每个组合对应的托管人和投管人名称,可采用自身关联查询:
SELECT X.O_CODE, X.O_NAME, X.O_FULLNAME, X.INVEST_MANAGER, X.DEPOSITER,
T.O_NAME AS TOU_ONAME, P.O_NAME AS TUO_ONAME
FROM TPRT X
LEFT JOIN TMANAGER T
ON X.INVEST_MANAGER = T.O_CODE AND
T.O_TYPE = '2'
LEFT JOIN TMANAGER P
ON X.DEPOSITER = P.O_CODE AND
P.O_TYPE = '3';