简单的 HQL/sql 查询..需要帮助吗?

发布于 2024-11-27 14:21:50 字数 732 浏览 0 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

九八野马 2024-12-04 14:21:50

在本机 SQL 中,这将是:

SELECT * 
FROM person 
LEFT JOIN (SELECT * 
           FROM vacations 
           WHERE startDate BETWEEN '2011-07-01' AND '2011-07-30')

未经测试,但您明白了:)

In native SQL this would be:

SELECT * 
FROM person 
LEFT JOIN (SELECT * 
           FROM vacations 
           WHERE startDate BETWEEN '2011-07-01' AND '2011-07-30')

untested, but you get the idea :)

樱花坊 2024-12-04 14:21:50

尝试以下操作,

<代码>
从 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.

宫墨修音 2024-12-04 14:21:50

您可能想使用函数来实现它:

select p from Person p 
where p.vacations.size = 0 
or (minelement(p.vacations) > '2011-07-01' 
    and 
    maxelement(p.vacations) < '2011-07-30')

这些日期实际上可能是日期类型。取决于类型。

尝试

select p from Person p 
where p.vacations.size = 0 
or (p.vacations.startDate > '2011-07-01' 
    and 
    p.vacations.endDate < '2011-07-30')

您还应该看看这里

You might want to use functions to achieve it:

select p from Person p 
where p.vacations.size = 0 
or (minelement(p.vacations) > '2011-07-01' 
    and 
    maxelement(p.vacations) < '2011-07-30')

Those dates might be actually a Date type. Depending of the type.

Try

select p from Person p 
where p.vacations.size = 0 
or (p.vacations.startDate > '2011-07-01' 
    and 
    p.vacations.endDate < '2011-07-30')

You should also take a look at here

岁月蹉跎了容颜 2024-12-04 14:21:50

在普通的 (My)SQL 中,它看起来像这样:

select distinct p.* from person p
left join vacations v on p.id = v.person_id 
where v.id is null or.v.start_date between '2011-07-01' and '2011-07-30'

在 HQL 中,这可能是(未测试)

select distinct p from Person as p 
left join fetch p.vacations as v 
where v.id is null or  v.startDate between '2011-07-01' and '2011-07-30'

或者也许(我不太确定这是否有效)

select distinct p from Person as p 
left join fetch p.vacations as v 
where v is null or v.startDate between '2011-07-01' and '2011-07-30'

In plain (My)SQL it would look like this:

select distinct p.* from person p
left join vacations v on p.id = v.person_id 
where v.id is null or.v.start_date between '2011-07-01' and '2011-07-30'

In HQL this will probably be (not tested)

select distinct p from Person as p 
left join fetch p.vacations as v 
where v.id is null or  v.startDate between '2011-07-01' and '2011-07-30'

or maybe (I'm not really sure if this will work)

select distinct p from Person as p 
left join fetch p.vacations as v 
where v is null or v.startDate between '2011-07-01' and '2011-07-30'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文