需求:
查询某条记录的所有父亲节点,或者所有孩子节点。
表结构如下:(id是当前节点,super_cust_id是父节点,-1表示根节点)

查询节点以及所有子节点sql语句:
<select id="queryNodesByParentId" resultMap="resultMap" parameterType="com.fh.iasp.app.cm.v2.custrel.pojo.BasCmCustomerRel">
WITH RECURSIVE cust AS (
SELECT
r.*
FROM
bas_cm_customer_rel r
WHERE
r.status = '1'
<if test="id != null">
and r.id = #{id,jdbcType=BIGINT}
UNION ALL
SELECT
rel.*
FROM bas_cm_customer_rel rel
JOIN cust AS B ON rel.super_cust_id = B. ID
</if>
) SELECT
cust.*
FROM cust;
</select>
查询节点以及所有父节点sql语句:
<select id="queryNodesByParentId" resultMap="resultMap" parameterType="com.fh.iasp.app.cm.v2.custrel.pojo.BasCmCustomerRel">
WITH RECURSIVE cust AS (
SELECT
r.*
FROM
bas_cm_customer_rel r
WHERE
r.status = '1'
<if test="id != null">
and r.id = #{id,jdbcType=BIGINT}
UNION ALL
SELECT
rel.*
FROM bas_cm_customer_rel rel
JOIN cust AS B ON rel.ID = B.super_cust_id
</if>
) SELECT
cust.*
FROM cust;
</select>

博客围绕查询某条记录的所有父亲节点或所有孩子节点的需求展开,给出了表结构,其中id为当前节点,super_cust_id为父节点,-1表示根节点,还提供了查询节点及所有子节点、父节点的sql语句。
5098





