自连接查询用于代替子查询
# 已知主单的DOCUMENT_ID
# 主单和分单的EXCHANGE_ID一致
# 查出主单(SUB_SEQ = 1)和分单(SUB_SEQ <> 1)
SELECT
H1.EXCHANGE_ID,
H1.DOCUMENT_ID,
H1.SUB_SEQ,
H2.EXCHANGE_ID,
H2.DOCUMENT_ID,
H2.SUB_SEQ
FROM
T_DOCUMENT_HEAD H1
INNER JOIN
T_DOCUMENT_HEAD H2
ON
H1.DOCUMENT_ID = '472e9959-4b52-4bac-a60d-07e83e924a3a'
AND
H1.EXCHANGE_ID = H2.EXCHANGE_ID;
表H1找出DOCUMENT_ID = '某值’的数据(为了得到EXCHANGE_ID)
表H2找出EXCHANGE_ID = '某值’的数据(最终目的)
表H1的EXCHANGE_ID等于表H2的EXCHANGE_ID
SELECT
H2.EXCHANGE_ID,
H2.DOCUMENT_ID,
H2.SUB_SEQ
FROM
T_DOCUMENT_HEAD H1
INNER JOIN
T_DOCUMENT_HEAD H2
ON
H1.DOCUMENT_ID = '472e9959-4b52-4bac-a60d-07e83e924a3a'
AND
H1.EXCHANGE_ID = H2.EXCHANGE_ID;