mysql - 使用resultmap时如何计算?
问题描述
我正在尝试使用 Mybatis resultmap 进行页面查询,我必须加入 Post,因为 where 子句中有一个条件,但是由于它是一对多关系,我怎样才能获得正确的 Blog 数量?
<select id="selectBlog" resultMap="blogResult">
select
B.id as blog_id,
B.title as blog_title,
B.author_id as blog_author_id,
P.id as post_id,
P.subject as post_subject,
P.body as post_body
from Blog B
left join Post P on B.id = P.blog_id
where B.id = #{id}
and P.subject=#{postSubject}
</select>
<resultMap id="blogResult" type="Blog">
<id property="id" column="blog_id" />
<result property="title" column="blog_title"/>
<collection property="posts" ofType="Post">
<id property="id" column="post_id"/>
<result property="subject" column="post_subject"/>
<result property="body" column="post_body"/>
</collection>
</resultMap>
编辑 对不起,我不清楚。这似乎很容易,我找到了解决方案:
<select id="count" resultType="java.lang.Integer">
select
count(distinct B.id)
from Blog B
left join Post P on B.id = P.blog_id
where B.id = #{id}
and P.subject=#{postSubject}
</select>
解决方案
推荐阅读
- java - 获取 FlywayException:SP2-0310:无法打开文件 - 当 sqlpus @ 与文件路径一起使用时
- typescript - 用于定义生成器的 Typescript 接口
- php - 如何扩展 Laravel 查询构建器?
- windows - 从 Powershell 中 Get-ItemProperty 的结果中检索特定字段
- cox-regression - coxph的输出中缺少R平方
- sql - 如何对数据库列进行分组?
- mysql - 选择具有不同条件的 2 列
- python - 在我使用方法移动对象后,有没有办法更新对象的原始 x 和 y 位置?
- spi - STM32F4上边界扫描内的SPI从机
- c# - 在纬度/经度折线上找到最近点的 C# 解决方案