我正在尝试生成一个输出,其中仅显示具有Surname =“Berry”课程的人员,其中日期按升序排列。
SELECT c.booking_number AS "Reference",
c.ldate AS "Lesson Date",
s.first_name AS "Instructor First Name",
s.surname AS "Instructor Last Name"
FROM am_lessons c
join am_staff s
ON ( c.staff_id = c.staff_id )
join am_bookings m
ON ( c.booking_number = m.booking_number )
join am_customers f
ON ( f.customer_id = c.customer_id )
WHERE f.surname = 'Berry'
ORDER BY c.ldate ASC
该声明会运行,但会重复出现“预订号码”,“名字”和“第二名”。
答案 0 :(得分:1)
首先,您有一些额外/不正确的连接:
SELECT c.booking_number AS "Reference",
c.ldate AS "Lesson Date",
s.first_name AS "Instructor First Name",
s.surname AS "Instructor Last Name"
FROM am_lessons c
join am_staff s
ON ( c.staff_id = s.staff_id ) -- should be s.Staff_Id
-- JOIN AM_Bookings m -- this join is not needed
-- ON (c.Booking_Number = m.Booking_Number)
join am_customers f
ON ( f.customer_id = c.customer_id )
WHERE f.surname = 'Berry'
ORDER BY c.ldate ASC
其次,您的表中可能有重复的行。如果是这种情况,请修复数据,或添加DISTINCT修饰符。
注意:AM_Bookings上的联接可能用于过滤掉没有关联的Booking_Number的记录。如果是这种情况,您只需过滤c.Booking_Number IS NOT NULL
。