【发布时间】:2013-02-26 13:43:59
【问题描述】:
这是我要执行的代码:
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 的情况下在 hibernate 中获得这种效果?
【问题讨论】: