当Mybatis+sql遇到Expected one result (or null) to be returned by selectOne(), but found: 2时

本文介绍了一个常见的 MyBatis + SQL 错误:“Expected one result (or null) to be returned by selectOne()...”的问题及解决办法。错误原因是期望通过 selectOne 方法返回单一结果时却查到了多条记录。文章详细解释了如何定位问题,并提供了一种通过增加 GROUP BY 子句来解决此问题的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



Expected one result (or null) to be returned by selectOne(), but found: 2

Mybatis+sql出现以上问题时,说明sql只允许查询一个值 selectOne, 但是查询的结果有两条

这时候你就要去你的sql中找到sql语句


select
a.id as "id",
a.name as "name",
a.original_price as "original_price",
a.price as "price",
a.content as "content",
a.sales as "sales",
a.classify as "classify",
a.specification as "specification",
a.unit as "unit",
a.rxsp as "rxsp",
a.remark as "remark",
a.tjr as "tjr",
a.tjsj as "tjsj",
a.xgsj as "xgsj",
b.tpdz as "tpdz",
ifnull(c.sl,0) as "sl"
from mall_goods a
left join mall_goods_photo b on b.goods_id=a.id
left join mall_goods_cart c on a.id=c.goods_id and c.zh=#{zh}
where a.id=#{id}
<if test="sfmr != null and sfmr != ''">
and b.sfmr=#{sfmr}
</if>
<if test="name != null and name != ''">
and b.name=#{name}
</if>
group by a.id
<if test="goods_id != null and goods_id != ''">
and b.goods_id=#{goods_id}
</if>

大概位置在where a.id=#{id}的地方



解决方法:

  在where id=#{id}的下面 加一句    group by a.id  然后问题应该就不会报错了, 问题解决了.


Expected one result (or null) to be returned by selectOne(), but found: 2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值