我正在尝试在 Hibernate 查询语言中执行 LEFT JOIN,在 MySQL 中我可以按如下方式执行此操作:
select * from day_timetable_timeslots t LEFT JOIN golfnine_date_time_entity d ON d.start_time = t.start_time
在我的表 day_timetable_timeslots 中,我有一整天的时间间隔,增量为 15 分钟。例如。 00:00:00, 00:15:00, 00:30:00, ... 到一天结束。
所以它向我展示了任何匹配的 Golfnine_date_time_entity 的所有时间间隔,但我似乎无法弄清楚如何在 Hibernate 查询语言中做到这一点。
好吧,我可以通过以下 HQL 进行 LEFT JOIN。
select o.id, book.id from DayTimetableTimeslots o left outer join o.bookings book where o.id > 0
我不知道为什么我必须把 o.id > 0 放在那里,但它有效。
但是,我只想选择预订具有 where 条件的 book.id。
我试过:
select o.id, book.id from DayTimetableTimeslots o left outer join o.bookings book where o.id > 0 and book.dateTime.startDate > '2010-01-01'
但这不能正常工作,它只显示了一些 DayTimetableTimeslots,而不是全部,因为它应该这样做。
我基本上想在 HQL 中做这个 mysql 查询。
选择 t.id 作为 start_time_sequence,t.start_time 作为 all_start_time,d.* from day_timetable_timeslots t LEFT JOIN Golfnine_date_time_entity d ON d.start_time = t.start_time AND t.customer_id = d.customer_id AND ='120-40-40 '
哪里 t.customer_id = 11
谢谢,菲利普
在 mysql 中,我可以执行以下操作,它会根据开始时间向我显示所有预订。所有开始时间都存储在 day_timetable_timeslots 中。
选择 t.start_time, d.id from day_timetable_timeslots t LEFT JOIN Golfnine_date_time_entity d ON d.start_time = t.start_time
'00:00:00', NULL
'00:15:00', NULL
'00:30:00', NULL
'00:45:00', NULL
'01:00:00', '443'
'01:15:00', NULL
'01:30:00', NULL
'01:45:00', NULL
'02:00:00', '444'
... 一整天,它将 Golfnine_date_time_entity 的 id 与 day_timetable_timeslots 中的时间相匹配。
这个 mysql 查询的好处是我可以在预订时设置一些标准,例如。
select t.id, t.start_time, d.id from day_timetable_timeslots t LEFT JOIN Golfnine_date_time_entity d ON d.start_time = t.start_time AND t.customer_id = d.customer_id AND d.start_date = '2010-01-t.'WHERE.24客户 ID = 11
我得到
... lots of data then
'31', '07:15:00', NULL
'32', '07:30:00', NULL
'33', '07:45:00', NULL
'34', '08:00:00', '501'
'35', '08:15:00', NULL
'36', '08:30:00', NULL
'37', '08:45:00', NULL
'38', '09:00:00', NULL
'39', '09:15:00', NULL
... lots more data
所以它只显示指定日期的预订和客户 ID。
在 HQL 中很难做到这一点......
这是我想要的 HQL 连接。
选择 o.id, b.id from DayTimetableTimeslots o, LegacyDateTimeEntity b 其中 b.customer.id = 11 and b.startDate = '2010-02-07' and o.startTime = b.startTime
给出这个 SQL。
select
daytimetab0_.id as col_0_0_,
legacydate1_.id as col_1_0_
from
day_timetable_timeslots daytimetab0_,
golfnine_date_time_entity legacydate1_
where
legacydate1_.customer_id=11
and legacydate1_.start_date='2010-02-07'
and daytimetab0_.start_time=legacydate1_.start_time
但是 - 它只返回 1 行,因为只有一个 Golfnine_date_time_entity 匹配,我希望返回所有 day_timetable_timeslots 行。
所以我尝试了 HQL。
从 DayTimetableTimeslots 中选择 o.id, o.bookings.size o left join o.bookings book left join book.dateTime dt with dt.customer.id = 11 and dt.startDate = '2010-02-29' where 1 = 1
可悲的是似乎忽略了 with 表达。
它返回此 SQL。
select
daytimetab0_.id as col_0_0_,
(select
count(bookings3_.timeslot_id)
from
golfnine_booking bookings3_
where
daytimetab0_.id=bookings3_.timeslot_id) as col_1_0_
from
day_timetable_timeslots daytimetab0_
left outer join
golfnine_booking bookings1_
on daytimetab0_.id=bookings1_.timeslot_id
left outer join
golfnine_date_time_entity legacydate2_
on bookings1_.date_time_id=legacydate2_.id
and (
legacydate2_.customer_id=11
and legacydate2_.start_date='2010-02-29'
)
where
1=1
它只是连接表之间的所有匹配关系并忽略
legacydate2_.customer_id=11
和 legacydate2_.start_date='2010-02-29'
我发现这似乎有效。注意我引用的是在 with 子句中的 dt2。
从 DayTimetableTimeslots 中选择不同的 o.startTime, dt2.id, book.uniqueDateTimeResource o left join o.bookings book with book.deleted = 0 left join book.dateTime dt2 with dt2.startDate = '2010-01-19' and dt2.deleted = 0
请您参考如下方法:
我可以想到您遇到的两个潜在问题:




