一切要从2019年说起 2021-11-18 17:30 采纳率: 100%
浏览 84
已结题

LEFT JOIN一对多引起的重复问题

问题阐述:

<sql id="Left_Child">
SELECT
    s.id,
    s.student_number,
    s.school_enrollment_number,
    s.school_id,
    s.student_name,
    s.sex,
    s.politic_countenance,
    s.nation,
    s.id_card,
     t.school_name,
    t.school_code,
    st.entrance_date,
    st.mailing_address,
    cu.guardian_address,
    cu.guardian_name,
    cu.guardian_telephone
FROM t_sch_student AS s
LEFT JOIN t_sch_school t on s.school_id = t.id and t.valid_flag = '1'
LEFT JOIN t_sch_student_status st on st.student_id = s.id and st.valid_flag = '1'
LEFT JOIN t_sch_student_custody cu on cu.student_id = s.id and cu.valid_flag = '1'
</sql>

t_sch_student_status、学生学籍表,t_sch_student_custody 学生监护信息表,,和学生基本表都是一对多的关系,如果用了left join,在导出excel的时候,一个学生有几个监护人,有几个学籍,就会导出几条数据,

img

但是如果不用left join,而是用 in,则导出无数据,SQL和结果如下:

<select id="selectAllStudentBaseAndChildInfo" resultType="com.rdp.project.manage.domain.SchStudentDO" resultMap="BaseResultMap">
    <include refid="Left_Child"/>
    <where>
        s.valid_flag = '1'
        <if test="r.studentNumber != null  and r.studentNumber != ''">
            and s.student_number like concat('%', #{r.studentNumber}, '%')
        </if>
        <if test="r.schoolId != null  and r.schoolId != ''">and s.school_id = #{r.schoolId}</if>
        <if test="r.studentName != null  and r.studentName != ''">
            and s.student_name like concat('%', #{r.studentName}, '%')
        </if>
        <if test="r.sex != null  and r.sex != ''">
            and s.sex = #{r.sex}
        </if>
        <if test="r.politicCountenance != null  and r.politicCountenance != ''">
            and s.politic_countenance = #{r.politicCountenance}
        </if>
        <if test="r.idCard != null  and r.idCard != ''">and s.id_card = #{r.idCard}</if>
         <if test="r.entranceDate != null  and r.entranceDate != ''">
            and s.id in (select st.student_id from  t_sch_student_status as st where
            st.valid_flag = '1' and st.entrance_date = #{r.entranceDate})</if>
        <if test="r.mailingAddress != null  and r.mailingAddress != ''">
            and s.id in (select st.student_id from  t_sch_student_status as st where
            st.valid_flag = '1' and st.mailing_address = #{r.mailingAddress})</if>
        <if test="r.guardianAddress != null  and r.guardianAddress != ''">
            and s.id in (select cu.student_id from  t_sch_student_custody as cu where
            cu.valid_flag = '1' and cu.guardian_address = #{r.guardianAddress})</if>
        <if test="r.guardianName != null  and r.guardianName != ''">
            and s.id in (select cu.student_id from  t_sch_student_custody as cu where
            cu.valid_flag = '1' and cu.guardian_name = #{r.guardianName})</if>
        <if test="r.guardianTelephone != null  and r.guardianTelephone != ''">
            and s.id in (select cu.student_id from  t_sch_student_custody as cu where
            cu.valid_flag = '1' and cu.guardian_telephone = #{r.guardianTelephone})</if>
        <if test="r.schoolIds != null">
            and school_id in
            <foreach collection="r.schoolIds" item="schoolId" open="("
                     separator="," close=")">
                #{schoolId}
            </foreach>
        </if>
    </where>
</select>

结果:字段导出无数据

img

  • 写回答

2条回答 默认 最新

  • 青山Magneto 2021-11-19 14:57
    关注

    你试试用inner join

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月24日
  • 已采纳回答 12月16日
  • 创建了问题 11月18日

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探