简体   繁体   English

Nhibernate简单的自我加入

[英]Nhibernate simple self join

I have a table with these fields: 我有一个包含这些字段的表:

ID (Primary key)
Name 
Some more data fields

I want to write a query that given a Name will give me all the rows with ID's bigger then the row with that name. 我想写一个给定名称的查询,该查询将给我所有ID大于该名称的行。

(Yes yes I know the name is not unique... in the system is.) (是的,我知道名称不是唯一的...在系统中是。)

I want something like: 我想要类似的东西:

select *
From SomeTable as x
WHERE x.ID> (Select ID from SomeTable as y where y.Name LIKE :param)

or: 要么:

SELECT x
FROM SomeTable as x
JOIN SomeTable as y ON x.ID > Y.ID
WHERE Y.Name LIKE :Param

Of course I want the self join, and not the sub query. 当然,我需要自我连接,而不是子查询。


BTW. 顺便说一句。 Criteria goes too... 标准也...

You won't be able to do it with a join in HQL. 您将无法通过加入HQL来做到这一点。

But this HQL query is OK: 但是此HQL查询是可以的:

select f from Foo f where f.id > (select f2.id from Foo f2 where f2.name = :name)

If the subselect might return several IDs, you may also use 如果子选择可能返回多个ID,则也可以使用

select f from Foo f where f.id > all (select f2.id from Foo f2 where f2.name = :name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM