这是我要执行的代码:
Query query = getSession().createQuery("select count(*),uv.voter.gender from UserVoter uv where " +
" uv.voter.age between "+startAge+" and "+endAge+" and uv.hamlet.hamletId = :hamletId group by uv .voter.gender ") ;
//query.setParameter("publicationDateId",publicationDateId);
query.setParameter("hamletId", hamletId);
return query.list();
以下是 start 和 end 参数的情况:
18-25
25-35
35-45
对于这 3 种情况,我必须运行 3 次查询。对于上述三种情况,我想在一次查询中获得结果。我有一个想法,new map()
但我该怎么做?
输出应映射到一对输入参数,例如:
18-25,55,male 18-25,56,female 25-35,44,male..
在mysql中我实现了这个
select concat(18,'to',25),count( v.voter_id),v.gender from voter v join user_voter u on v.voter_id = u.voter_id
where v.age between 18 and 25 and u.hamlet_id=10 group by v.gender
union
select concat(26,'to',35),count( v.voter_id),v.gender from voter v join user_voter u on v.voter_id = u.voter_id
where v.age between 26 and 35 and u.hamlet_id=10 group by v.gender
union
select concat(36,'to',50),count( v.voter_id),v.gender from voter v join user_voter u on v.voter_id = u.voter_id
where v.age between 36 and 50 and u.hamlet_id=10 group by v.gender
union
select concat(51,'to',200),count( v.voter_id),v.gender from voter v join user_voter u on v.voter_id = u.voter_id
where v.age between 51 and 200 and u.hamlet_id=10 group by v.gender;
但是如何在不使用本机 sql 的情况下在休眠中获得这种效果?