Hql语句注意事项总结
2007-06-01 12:59
363 查看
1.在Hql中使用group by的问题
(1)使用统计函数作为输出结果
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select sum(total),sum(totalup) from AD where aid=? and fid=? and vdatetime>=? and vdatetime<=? group by aid
(2)使用统计函数作为查询约束条件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select uid from MM group by uid having count(*)>? and min(m_date)<?
(3)使用group by 时有关约束条件的问题
如果约束条件是单个字段,则必须用where,并且where要放在group by之前,如:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select cityname from MI where vdatetime>=? and vdatetime<? group by cityname
如果约束条件是统计函数,则要使用having,并且having要放在group by之后,如:(2)
(4)其实,在某些场合,group by可以用distinct代替,如以下两条语句功能相同:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select cityname from MI where vdatetime>=? and vdatetime<? group by cityname
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select distinct cityname from MI where vdatetime>=? and vdatetime<?
2.在count()中使用别名问题
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select count(*) from MipMailconfig as MipMailconfig where MipMailconfig.uid <> ? and MipMailconfig.email = ?-- 正确
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select count(MipMailconfig.*) from MipMailconfig as MipMailconfig where MipMailconfig.uid <> ? and MipMailconfig.email = ?-- 错误
3.关于Integer和Long的问题
在Hibernate3.2中,所有的计数都要用Long,用Integer的话就会有问题.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
List list=this.find("select count(*) from MMC as MMC where MMC.uid <> ? and MMC.email = ?",new String[]...{uid,email});
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
if(list!=null&&!list.isEmpty())...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
long count=Long.parseLong(list.get(0).toString());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(count>0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
4.关于参数是数组的问题
在实际的工作中,经常遇到这种情况:
(1)要删除一批数据,传过来一个数组,
(2)或者要查询一批数据,查询条件也是传过来一个数组,
这种情况下如何进行处理?
(1)查询数据,查询条件为一个数组,可以有如下两个方法:
<1>直接拼Hql语句
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
String[] ids = ...{"1","2","3"};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
String str="";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
for(int i=0;i<ids.length;i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str+="'"+ids[i]+"'";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(i!=(ids.length-1))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str+=",";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
List list;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
list = appinfoManager.find("from Appinfo where id in ("+str+")");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
if(list!=null)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(int i=0;i<list.size();i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Appinfo app = (Appinfo)list.get(i);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(app.getAppname());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
System.out.println("共有"+list.size()+"条记录");
<2>利用回调函数处理
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public List getApList() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
return (List)this.getHibernateTemplate().execute(new HibernateCallback() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Object doInHibernate(Session session) throws HibernateException, SQLException ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
String[] ids = ...{"1","2","3"};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String hql= "from Appinfo where id in (:ids)";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Query query = session.createQuery(hql);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
List list = query.setParameterList("ids", ids).list();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return list;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
});
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
(2)删除数据,参数为数组,利用回调函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public void delInArray() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
this.getHibernateTemplate().execute(new HibernateCallback() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Object doInHibernate(Session session) throws HibernateException, SQLException ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
String[] ids = ...{"1","2","3"};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String hql= "delete Appinfo where id in (:ids)";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Query query = session.createQuery(hql);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
query.setParameterList("ids", ids).executeUpdate();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
});
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
http://blog.csdn.net/hbcui1984/archive/2006/12/05/1431011.aspx
(1)使用统计函数作为输出结果
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select sum(total),sum(totalup) from AD where aid=? and fid=? and vdatetime>=? and vdatetime<=? group by aid
(2)使用统计函数作为查询约束条件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select uid from MM group by uid having count(*)>? and min(m_date)<?
(3)使用group by 时有关约束条件的问题
如果约束条件是单个字段,则必须用where,并且where要放在group by之前,如:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select cityname from MI where vdatetime>=? and vdatetime<? group by cityname
如果约束条件是统计函数,则要使用having,并且having要放在group by之后,如:(2)
(4)其实,在某些场合,group by可以用distinct代替,如以下两条语句功能相同:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select cityname from MI where vdatetime>=? and vdatetime<? group by cityname
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select distinct cityname from MI where vdatetime>=? and vdatetime<?
2.在count()中使用别名问题
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select count(*) from MipMailconfig as MipMailconfig where MipMailconfig.uid <> ? and MipMailconfig.email = ?-- 正确
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select count(MipMailconfig.*) from MipMailconfig as MipMailconfig where MipMailconfig.uid <> ? and MipMailconfig.email = ?-- 错误
3.关于Integer和Long的问题
在Hibernate3.2中,所有的计数都要用Long,用Integer的话就会有问题.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
List list=this.find("select count(*) from MMC as MMC where MMC.uid <> ? and MMC.email = ?",new String[]...{uid,email});
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
if(list!=null&&!list.isEmpty())...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
long count=Long.parseLong(list.get(0).toString());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(count>0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
4.关于参数是数组的问题
在实际的工作中,经常遇到这种情况:
(1)要删除一批数据,传过来一个数组,
(2)或者要查询一批数据,查询条件也是传过来一个数组,
这种情况下如何进行处理?
(1)查询数据,查询条件为一个数组,可以有如下两个方法:
<1>直接拼Hql语句
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
String[] ids = ...{"1","2","3"};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
String str="";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
for(int i=0;i<ids.length;i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str+="'"+ids[i]+"'";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(i!=(ids.length-1))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
str+=",";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
List list;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
list = appinfoManager.find("from Appinfo where id in ("+str+")");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
if(list!=null)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
for(int i=0;i<list.size();i++)...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Appinfo app = (Appinfo)list.get(i);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(app.getAppname());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
System.out.println("共有"+list.size()+"条记录");
<2>利用回调函数处理
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public List getApList() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
return (List)this.getHibernateTemplate().execute(new HibernateCallback() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Object doInHibernate(Session session) throws HibernateException, SQLException ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
String[] ids = ...{"1","2","3"};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String hql= "from Appinfo where id in (:ids)";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Query query = session.createQuery(hql);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
List list = query.setParameterList("ids", ids).list();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return list;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
});
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
(2)删除数据,参数为数组,利用回调函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public void delInArray() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
this.getHibernateTemplate().execute(new HibernateCallback() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Object doInHibernate(Session session) throws HibernateException, SQLException ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
String[] ids = ...{"1","2","3"};
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String hql= "delete Appinfo where id in (:ids)";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Query query = session.createQuery(hql);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
query.setParameterList("ids", ids).executeUpdate();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
});
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
http://blog.csdn.net/hbcui1984/archive/2006/12/05/1431011.aspx
相关文章推荐
- Hql语句注意事项总结 批量删除 批量查询
- Hql语句注意事项总结
- Hql语句注意事项总结
- Hql语句注意事项总结
- try{} catch{} finally{} 语句注意事项总结
- try{} catch{} finally{} 语句注意事项总结
- HQL语句书写注意事项
- 总结新系统部署时,Oracle的一些注意事项:
- hive的查询注意事项以及优化总结 .
- select语句 注意事项
- 个推接入注意事项总结
- winform总结5> winform程序开发注意事项
- SSDB 安装部署及注意事项总结
- 学习项目代码总结(一)—— ListView的自定义Adapter的性能提升与注意事项
- HQL语句总结
- 关于使用READ TABLE语句的几点注意事项
- javascript语言使用技巧及注意事项总结
- 在PB中动态修改数据窗口DW的SQL语句方法注意事项
- hql单列查询注意事项
- openswan ipsec使用注意事项总结