Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server Left Join过滤掉了我希望在使用HQL时显示的行_Sql Server_Nhibernate_Hql_Left Join - Fatal编程技术网

Sql server Left Join过滤掉了我希望在使用HQL时显示的行

Sql server Left Join过滤掉了我希望在使用HQL时显示的行,sql-server,nhibernate,hql,left-join,Sql Server,Nhibernate,Hql,Left Join,我有一些类似于以下HQL的内容: select count(distinct car.id), count(distinct w1.id), count(distinct w2.id), count(distinct w3.id) from Car car left join Wheel w1 left join Wheel w2 with w2.IsFrontWheel == true left join Wheel w3 with w3.IsFrontWheel == false wh

我有一些类似于以下HQL的内容:

select count(distinct car.id), 
count(distinct w1.id), 
count(distinct w2.id), 
count(distinct w3.id)
from Car car
left join Wheel w1
left join Wheel w2 with w2.IsFrontWheel == true
left join Wheel w3 with w3.IsFrontWheel == false
where w1.ManufacturedDate > :manufacturedDate
and w2.ManufacturedDate > :manufacturedDate
and w3.ManufacturedDate > :manufacturedDate
group by car.Make
此HQL是在我的用户运行报表时通过他们的选择动态生成的。这里需要注意的重要一点是,用户正在动态地选择筛选,这导致此HQL动态地构建where子句

我的where子句生成工作正常

我的问题是,如果在SQL Server 2008中运行结果查询,最终结果是,如果存在没有任何大于给定参数的ManufacturedDate的控制盘w1、w2或w3,则报表将完全忽略该记录

我发现了原因

我的问题是,我无法将这些过滤器添加到HQL中的with子句中。在上面的例子中,我可以将约束添加到With子句中,但是我构建的报表引擎让用户执行比此更高级的过滤,其中大部分是With子句中不允许的。我曾经尝试将一些生成的HQL放入with中,并验证了这一点,其中大多数查询不会运行,因为它们需要对其他表进行额外的约束

所以我的问题是:

有没有办法修改此HQL以找回丢失的记录?在我上面列出的文章中,有人提到,如果你考虑到空值,你可以把记录拿回来,但我不知道他指的是什么,如果可能的话,我肯定想知道怎么做

更新

我被迫将动态过滤器放在where子句中,因为用户也可能会执行以下操作:

select count(distinct car.id), 
count(distinct w1.id), 
count(distinct w2.id), 
count(distinct w3.id)
from Car car
left join Wheel w1
left join Wheel w2 with w2.IsFrontWheel == true
left join Wheel w3 with w3.IsFrontWheel == false
where w1.Buyer.FirstName = :firstName
and w2.Buyer.FirstName = :firstName
and w3.Buyer.FirstName = :firstName
group by car.Make
如果我重写with子句

select count(distinct car.id), 
count(distinct w1.id), 
count(distinct w2.id), 
count(distinct w3.id)
from Car car
left join Wheel w1 with w1.Buyer.FirstName = :firstName
left join Wheel w2 with w2.IsFrontWheel == true 
    and w2.Buyer.FirstName = :firstName
left join Wheel w3 with w3.IsFrontWheel == false 
    and w3.Buyer.FirstName = :firstName
group by car.Make

无法运行,NHibernate抛出异常。

请检查HQL中的“with”关键字

您的问题是筛选WHERE子句中的左连接列:

select count(distinct car.id), 
count(distinct w1.id), 
count(distinct w2.id), 
count(distinct w3.id)
from Car car
left join Wheel w1
left join Wheel w2 with w2.IsFrontWheel == true
left join Wheel w3 with w3.IsFrontWheel == false
where w1.ManufacturedDate > :manufacturedDate --<<<<
and w2.ManufacturedDate > :manufacturedDate   --<<<<
and w3.ManufacturedDate > :manufacturedDate   --<<<<
group by car.Make
当您比较w2.ManufacturedDate>:ManufacturedDate时,它就像执行null>:ManufacturedDate,结果为null,并且该行被丢弃,就像是一个内部联接。将条件移动到左侧联接的启用位置可防止出现这种情况

编辑 试试这个:

select count(distinct car.id), 
count(distinct w1.id), 
count(distinct w2.id), 
count(distinct w3.id)
from Car car
left join Wheel w1
left join Wheel w2 with w2.IsFrontWheel == true
left join Wheel w3 with w3.IsFrontWheel == false
where ISNULL(w1.ManufacturedDate,'99991231') > :manufacturedDate
and ISNULL(w2.ManufacturedDate,'99991231)' > :manufacturedDate
and ISNULL(w3.ManufacturedDate,'99991231') > :manufacturedDate
group by car.Make

虽然我对HQL一无所知,但我还是要深入研究:

如果您需要避免使用WITH,那么当您想要计算的结果发生时,您需要WHERE子句为true。从你的问题来看,我并不完全清楚这种情况,但类似于:

where ISNULL(w1.ManufacturedDate, *some_future_value*) > :manufacturedDate
…其中,*some_future_value*是您以某种方式生成的一个值,它比manufacturedDate参数更接近未来。因此,可能是您的最大可能日期值,或:manufacturedDate+1',或任何适合您的值。

尝试以下操作:

left join Wheel w1
left join w1.Buyer b1 with b1.FirstName = :firstName

其他的都一样。应该可以。

我知道with关键字,我的示例甚至使用了它。但不能在SelectID fromBuyer中使用w1.Buyer.id,其中FirstName=:firstName@KM我已经运行了这个程序,但对于更复杂的事情,HQL会抛出一个异常。我将更新我的问题以反映导致我出现问题的HQL。@KM您的编辑不起作用,它仍在过滤recordstry,将“99991231”替换为“18000101”,谢谢,但我得到的结果与where clauseWell中的结果相同,这是在上次查询中生成左连接的正确方法。但也许这对你想要得到的还不够。尝试编写查询应该返回的功能规范。
left join Wheel w1
left join w1.Buyer b1 with b1.FirstName = :firstName