我继承了以下数据库和以下查询,该查询提取这些程序具有订阅者的程序名称。
SELECT DISTINCT label
FROM program
WHERE EXISTS (
SELECT occurrence.uuid
FROM booking
INNER JOIN enrolment on enrolment.booking = booking.uuid
INNER JOIN occurrence on occurrence.id = enrolment.occurrence
AND occurrence.programme = programme.uuid
AND booking.status IN ('completed','booked')
)
以下是简化的相关表格。
每个程序都有一个programType。对于所有已预订的程序,我需要修改上面的查询以从ProgramType而不是Program获取标签。
**Program**
ID UUID Label ProgramType
---------------------------------------------------------------------------------
12 04FE546E-DD70-AAA3-2DB0DB3246790967 Prog1 8BA7E719-19B9-EFA7-D6937B42D3A4A056
32 04FE54FD-E8C5-9B02-2A08BBC22E630F09 Prog2 6EA6CB18-0E3F-360F-8BA8468E406C1EAB
**ProgramType**
ID Label
------------------------------------------
8BA7E719-19B9-EFA7-D6937B42D3A4A056 Type1
6EA6CB18-0E3F-360F-8BA8468E406C1EAB Type2
**Booking**
UUID Label Occurrence Status
------------------------------------------------------------------------
102B22F0-19B9-EFA7-D66E9E6746B5CC64 Booking1 73 Completed
C8930CB4-19B9-EFA7-D6A15045AFF02FA4 Booking2 15 Booked
**Occurrence**
(One program can have different dates etc)
ID UUID Program StartDate
-------------------------------------------------------------------------------------------
73 102B22F0-19B9-EFA7-D66E9E6746B5CC64 8BA7E719-19B9-EFA7-D6937B42D3A4A056 2010-11-05
15 C8930CB4-19B9-EFA7-D6A15045AFF02FA4 8BA7E719-19B9-EFA7-D6937B42D3A4A056 2010-11-12
**Enrolment**
(Fully enrolled once paid)
ID UUID Program PaymentID
-------------------------------------------------------------------------------------------
73 31D5680F-00EF-349C-E00F8A425EF531E3 31E84300-D8EB-4E9C-9CD8F87B47CABD84 100027
15 31E8543A-A810-DB22-C5EE25B8FF8BE3CA 31F2F89C-A88B-3B2C-53995F03C8276C5A 100029
答案 0 :(得分:0)
您可以通过在查询中添加联接来执行此操作:
SELECT DISTINCT ProgramType.label
FROM program join
programType
on program.ProgramType = ProgramType.Id
WHERE EXISTS (SELECT occurrence.uuid
FROM booking INNER JOIN
enrolment
on enrolment.booking = booking.uuid INNER JOIN
occurrence
on occurrence.id = enrolment.occurrence AND
occurrence.programme = programme.uuid AND
booking.status IN ('completed','booked')
)