6.4.11:子查询

来源:互联网 发布:mac os x使用手册 编辑:程序博客网 时间:2024/06/05 04:39

如果底层数据库支持子查询,则可以在HQL语句中使用子查询。与SQL中子查询相似的是,HQL中的子查询也需要使用英文括号括起来,例如:

from Cat as fatcatwhere fatcat.weight>(select avg(cat.weight) from DomesticCat cat);
与SQL子查询语法完全类似,如果子查询是多行结果集,则应该使用多行运算符,例如:

from Cat as catwhere not (cat.name,cat.color) in(select cat.name,cat.color from DomesticCat cat);from DomesticCat as catwhere cat.name not in(select name.nickName from Name as name);
SQL语法中子查询还可以出现在select子句之后,HQL也支持这种用法,例如:

select cat.id,(select max(kit.weight) from cat.kitten kit)from Cat as cat;
如果在select子查询后的列表中包含多项,则在HQL中需要使用一个元组构造符,例如:

from Cat as catwhere not (cat.name,cat.color) in(select cat.name,cat.color from DomesticCat cat);


原创粉丝点击