public List<Integer> findUnionCount() {
// TODO Auto-generated method stub
/*select count(*) from i_email where 1=1 and
birthday between '1991-01-01 00:00:00'
and '1992-12-31 23:59:59'
union all
select count(*) from i_email where 1=1 and
birthday between '1980-01-01 00:00:00'
and '2013-12-31 23:59:59'*/
StringBuffer buffer = new StringBuffer("");
buffer.append("Select count(*) from i_email");
buffer.append(" where 1=1");
buffer.append(" and birthday between '1991-01-01 00:00:00' and '1992-12-31 23:59:59'");
buffer.append(" union all");
buffer.append(" Select count(*) from i_email");
buffer.append(" where 1=1");
buffer.append(" and birthday between '1980-01-01 00:00:00' and '2013-12-31 23:59:59'");
//queryObject(buffer.toString(), null);
Session session = this.getHibernateTemplate().getSessionFactory().openSession();
SQLQuery sQLQuery = session.createSQLQuery(buffer.toString());
List<Object> list = sQLQuery.list();
for(int i=0;i<list.size();i++){
System.out.println((BigInteger)list.get(i));
}
return null;
}
這裏的Union All 只是一個彙總字段
Console
如果是多個字體就用
for(int i=0;i<list.size();i++){
Object[] o = (Object[]) list.get(i);
System.out.println("City Id: " + o[0] + " || Count: " + o[1]);
}