LEFT JOIN 右表数据多条导致查询重复解决办法

相信大家都遇到过。A表有2条数据,B表有10条数据,A,B LEFT JOIN 查出来的数据不是2条。而是多条,遇到这种情况怎么办的。可以在代码中控制一下。

resultType
resultType可以把查询结果封装到pojo类型中,但必须pojo类的属性名和查询到的数据库表的字段名一致。 
如果sql查询到的字段与pojo的属性名不一致,则需要使用resultMap将字段名和属性名对应起来,进行手动配置封装,将结果映射到pojo中

resultMap

resultMap可以实现将查询结果映射为复杂类型的pojo,比如在查询结果映射对象中包括pojo和list实现一对一查询和一对多查询。

数据库字段:user_id,

实体类字段:userId

需要手动配置设置resultMap

Mapper中基本查询语句

<!-- 查询所有的订单数据 -->
    <!-- resultMap:填入配置的resultMap标签的id值 -->
    <select id="queryOrderAll" resultMap="orderResultMap">
        SELECT id, user_id,
        number,
        createtime, note FROM `order`
    </select>
resultMap中字段映射

<!-- resultMap最终还是要将结果映射到pojo上,type就是指定映射到哪一个pojo -->
    <!-- id:设置ResultMap的id -->
    <resultMap type="order" id="orderResultMap">
        <!-- 定义主键 ,非常重要。如果是多个字段,则定义多个id -->
        <!-- property:主键在pojo中的属性名 -->
        <!-- column:主键在数据库中的列名 -->
        <id property="id" column="id" />
 
        <!-- 定义普通属性 -->
        <result property="userId" column="user_id" />
        <result property="number" column="number" />
        <result property="createtime" column="createtime" />
        <result property="note" column="note" />
    </resultMap>

例子2

mapper.xml中

    <resultMap id="BaseResultMap" type="com.hisense.sys.entity.MyClothes">
        <id column="id" property="id" />
        <result column="user_id" property="userId" />
        <result column="clothes_id" property="clothesId" />
        <result column="clothes_name" property="clothesName" />
        <result column="big_class_label" property="bigClassLabel" />
        <result column="big_class_name" property="bigClassName" />
        <result column="small_class_id" property="categoryId" />
        <result column="small_class_label" property="categoryName" />
        <result column="small_class_name" property="smallClassName" />
        <result column="clothes_pic_url" property="thumbnail" />
        <result column="clothes_gender" property="clothesGender" />
        <result column="rfid" property="rfid" />
        <result column="rfid_last_four" property="rfidLastFour" />
        <result column="clothes_type" property="clothesType" />
        <result column="can_try" property="canTry" />
        <result column="can_buy" property="canBuy" />
        <result column="minipro_url" property="miniproUrl" />
        <result column="status" property="status" />
        <result column="create_date" property="createDate" />
        <result column="color_id" property="colorId" />
        <result column="virtual_flag" property="virtualFlag" />
        <result column="season" property="season" />
        <collection property="myClothesDetailList"  ofType="com.hisense.sys.entity.MyClothesDetail">
            <result property="clothesPropertyCode" column="clothesPropertyCode" />
            <result property="clothesPropertyType" column="clothesPropertyType" />
            <result property="clothesPropertyName" column="clothesPropertyName" />
        </collection>
    </resultMap>
<select id="getMyClothesList" parameterType="map" resultMap="BaseResultMap">
        SELECT
    t.user_id,t.season,
            t.id,
            t.clothes_name,
            t.clothes_id,
            t.color_id,
            t.big_class_label,
            t.big_class_name,
            t.small_class_label,
            t.small_class_name,
            t.create_date,
            t.clothes_pic_url,
            t.small_class_id,
            t.clothes_type,
            t.clothes_gender,
            t.can_buy,
            t.can_try,
            t.minipro_url,
            t.rfid,
            t.rfid_last_four,
            t1.clothes_property_code  clothesPropertyCode,
            t1.clothes_property_name  clothesPropertyName,
            t1.clothes_property_type clothesPropertyType
        FROM `t_my_clothes` t LEFT JOIN t_my_clothes_detail t1 ON t.id = t1.my_clothes_id
        where t.id in( SELECT temp.id from (select t2.id from t_my_clothes t2 where   t2.status = '0' and t2.clothes_gender = #{sex}
		
		<if test="sex!=null and sex !=0">
            and ( t2.user_id =  #{userId} or  t2.user_id ='da89a033-7261-4351-84cb-56375649d3f3')
        </if>
        <if test="sex!=null and sex ==0">
            and ( t2.user_id =  #{userId} or  t2.user_id ='488be7e1-a890-46a5-9a8f-807fd29c60a1')
        </if>
		
        <if test="bigClassLabel!=null and bigClassLabel !=''">
            and t2.big_class_label = #{bigClassLabel}
        </if>
        <if test="smallClassLabel!=null and smallClassLabel !=''">
            and t2.small_class_label =#{smallClassLabel}
        </if>
        <if test="season!=null and season !=''">
            and t2.season =#{season}
        </if>
        <if test="pageNum!=null and pageNum !=''">
            limit #{pageNum},#{pageSize}
        </if>
        ) as temp )
        <if test="checkFlag!=null and checkFlag !=''">
            and t.virtual_flag = #{checkFlag}
        </if>
        order by t.create_date desc
    </select>

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
左连接(left join)是一种关系型数据库中的连接(join)操作,它包含了左边表中的所有记录,即使右边表中没有与之匹配的记录。当左表的一记录在右表中有多条匹配记录时,这一对多的关系会导致左表的这记录在结果中被重复出现多次。 为了解决这个问题,可以使用group by字段来保证结果中的每记录具有唯一性。通过对group by字段进行分组,可以将重复的记录合并为一。这样就可以避免左表的记录在结果中出现多次。在具体的SQL语句中,可以通过使用LEFT JOIN关键字来进行左连接操作。但需要注意的是,不同的数据库可能对SQL语法的支持有所差异,所以请根据自己使用的数据库来编写对应的SQL语句。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [左连接与右连接 ](https://blog.csdn.net/shanliwa/article/details/1767442)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [关于左连接left join查出比原左表数据多](https://blog.csdn.net/m0_57661807/article/details/125043301)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yuanshiren133

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值