group by
单表
sql返回多个列的 都是用 List<Map<String, Long>>
,注意:返回的计算结果都是Long类型
@MapKey("ltStatus")
List<Map<String, Object>> groupBy1();
List<Map<String, Object>> groupBy2();
<select id="groupBy1" resultType="java.util.Map">
select login_name as lt_status, count(*)
from `user`
group by lt_status
</select>
<select id="groupBy2" resultType="java.util.Map">
select login_name, count(*)
from `user`
group by login_name
</select>
@MapKey是group by的字段驼峰命名
groupBy1,map有两个元素
- lt_status为key,value是login_name的字段值
- count(*)为key,value是计算的结果
groupBy2,map有连个元素
- login_name为key,value是 login_name字段值
- count(*)为key,value是计算的结果
@MapKey("userId")
List<Map<String, Object>> guanlian();
@MapKey("userId")
List<Map<String, Object>> guanlian2();
<select id="guanlian" resultType="java.util.Map">
select o.user_id, count(1)
from `order` o, `user` u
where u.user_id = o.user_id
group by o.user_id
</select>
<select id="guanlian2" resultType="java.util.Map">
select o.user_id , count(1) as num
from `order` o, `user` u
where u.user_id = o.user_id
group by o.user_id
</select>
guanlian
- user_id为key
guanlian2
- 第一个元素 user_id为key
- 第二个元素 num为key
尽量查询出来 在service层使用stream进行分组
相关文章
暂无评论...