简单的 HQL/sql 查询..需要帮助吗?
我有 2 个表(实体),一个人员实体和一个假期实体。一个人可以有很多假期。
我想做的是以下查询:
选择所有人员的时间段!,但是对于有假期的人员,我想获取所有有条件的人员假期,以免只说 startDate< /code> 介于某物之间。
需要明确的是,无论假期有什么限制,我仍然想获取所有人员。这是我当前的 HQL 查询:
select distinct p
from Person as p
left join fetch p.vacations as v
where v.startDate between '2011-07-01' and '2011-07-30'
它的问题是它限制了我的人员,仅返回那些有符合条件的假期的人员....有人可以帮忙吗?
如果有人能在本机 SQL 中告诉我这一点,我没有问题。
更新在本机 SQL 中解决,感谢@Quasdunk:
SELECT *
FROM person as p
LEFT JOIN
(SELECT * FROM vacation
WHERE startDate BETWEEN '2011-08-01' AND '2011-08-30') as v ON v.person_id = p.id
我的新问题是如何将其转换为 HQL 查询,以便我可以获取每个人的假期以及要参加的人的假期。不同(条件保持不变)。
I have 2 tables (entity) a person entity and a vacation entity. One person can have many vacations.
What I'm trying to do is the following query:
Select all the persons period!, but for the persons who have a vacations, I want to fetch all of the persons vacations which have a condition , lest just say startDate
is between something.
Just to be clear no matter the constraints on the vacations I still want to get all the persons.this is my current HQL query:
select distinct p
from Person as p
left join fetch p.vacations as v
where v.startDate between '2011-07-01' and '2011-07-30'
The problem with it is that it constraint my persons an returns only the ones who have a vacation that respects the condition....Can someone help please?
I have no problem if someone can tell me this in native SQL
UPDATE Resolved in native SQL thanks to @Quasdunk:
SELECT *
FROM person as p
LEFT JOIN
(SELECT * FROM vacation
WHERE startDate BETWEEN '2011-08-01' AND '2011-08-30') as v ON v.person_id = p.id
My new problem is how to convert it to an HQL query so I can fetch the vacations for each person, and the persons to be distinct (the conditions remain the same).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在本机 SQL 中,这将是:
未经测试,但您明白了:)
In native SQL this would be:
untested, but you get the idea :)
尝试以下操作,
<代码>
从 PERSON p LEFT JOIN VACATION v 中选择 p.*、v.*
上 (p.id = v.p_id)
其中 v.startDate 介于“2011-07-21”和“2011-07-22”之间
希望这对您有用。
Try the following,
select p.*, v.* from PERSON p LEFT JOIN VACATION v
on (p.id = v.p_id)
where v.startDate between '2011-07-21' and '2011-07-22'
Hope this works for you.
您可能想使用函数来实现它:
这些日期实际上可能是日期类型。取决于类型。
尝试
您还应该看看这里
You might want to use functions to achieve it:
Those dates might be actually a Date type. Depending of the type.
Try
You should also take a look at here
在普通的 (My)SQL 中,它看起来像这样:
在 HQL 中,这可能是(未测试)
或者也许(我不太确定这是否有效)
In plain (My)SQL it would look like this:
In HQL this will probably be (not tested)
or maybe (I'm not really sure if this will work)