现在有一张food_order表
select count(1),user_id from food_order where
create_time >= '2019-03-18 15:01:53' and create_time<='2019-03-20 13:18:41' and order_status in (-2,1,2,3,4)
and user_id in (select DISTINCT(user_id) from food_order where create_time >= '2019-03-18 15:01:53' and create_time<= '2019-03-20 13:18:41'
and order_status in (-2,1,2,3,4))
group by user_id;
查询出的结果是
如果有多条数据的话
mapper.xml
<sql id="countOrderUser_where_if">
where
1=1
<if test="beginDate1 != null">
and create_time >= #{beginDate1}
</if>
<if test="endDate1 != null">
and create_time <= #{endDate1}
</if>
and order_status in (-2,1,2,3,4)
</sql>
<select id="countOrderUserByCreateTime" resultType="java.util.HashMap">
select user_id as 'key',count(1) as 'value' from food_order
<include refid="countOrderUser_where_if"></include>
and user_id in (select DISTINCT(user_id) from food_order
<include refid="countOrderUser_where_if"></include>)
group by user_id;
</select>
dao层
List<HashMap<String,Object>> countOrderUserByCreateTime
(@Param("beginDate1") Date beginDate1, @Param("endDate1")Date endDate1);
controller
Map<Long, Long> map = new HashMap<>();
List<HashMap<String, Object>> list=
foodOrderDao.countOrderUserByCreateTime(beginDate1,endDate1);
if (list != null && !list.isEmpty()) {
for (HashMap<String, Object> map1 : list) {
Long key = null;
Long value = null;
for (Map.Entry<String, Object> entry : map1.entrySet()) {
if ("key".equals(entry.getKey())) {
key = (Long) entry.getValue();
} else if ("value".equals(entry.getKey())) {
value = (Long) entry.getValue();
}
}
map.put(key, value);
}
}
不要自己写按照博主的来否则会报类转化异常 亲身经历 前方有坑