减少SQL查询中的重复结果

时间:2017-12-12 02:54:51

标签: sql mysql-management

    select
rtrim(p.firstname) + ' ' + p.lastname as [instructor name], 
lesson_type.lessonname, 
location.locationname, 
lesson_1.lessondate
from person as p inner join lesson as l 
on p.personid = l.employeeid 
inner join location 
on l.locationid = location.locationid 
inner join lesson as lesson_1 
on p.personid = lesson_1.employeeid and location.locationid = lesson_1.locationid 
inner join lesson_type 
on l.lessontypeid = lesson_type.lessontypeid and lesson_1.lessontypeid = lesson_type.lessontypeid
order by [instructor name] ASC

如何为相同的位置名称和课程名称重复课程日期?这是我的输出:

instructor name                 lessonname      locationname    lessondate
------------------------------- --------------- --------------- ----------
Anna Dillinger                  Diamond         Brighton Ski    2014-01-12
Anna Dillinger                  Newbie          Steamboat       2014-01-01
Anna Dillinger                  Intermediate    Steamboat       2014-01-01
Anna Dillinger                  Newbie          Mt. Holly       2014-02-12
Anna Dillinger                  Newbie          Mt. Holly       2014-02-12
Anna Dillinger                  Newbie          Mt. Holly       2014-02-12
Anna Dillinger                  Newbie          Mt. Holly       2014-02-12
Jeff Gregory                    Cross Country   Snow Snake      2013-12-23
Jeff Gregory                    Advanced        Pine Knob       2013-12-12
Jeff Gregory                    Advanced        Pine Knob       2013-12-12
Jeff Gregory                    Advanced        Pine Knob       2013-12-12
Jeff Gregory                    Advanced        Pine Knob       2013-12-12
Jeff Gregory                    Intermediate    Pine Knob       2013-12-12
Jeff Gregory                    Intermediate    Pine Knob       2013-12-12
Jeff Gregory                    Intermediate    Pine Knob       2013-12-12
Jeff Gregory                    Intermediate    Pine Knob       2013-12-12

1 个答案:

答案 0 :(得分:2)

删除重新加入的lesson_1可能有帮助

SELECT
      RTRIM(p.firstname) + ' ' + p.lastname AS [instructor name]
    , lesson_type.lessonname
    , location.locationname
    , l.lessondate
FROM person AS p
INNER JOIN lesson AS l ON p.personid = l.employeeid
INNER JOIN location ON l.locationid = location.locationid
INNER JOIN lesson_type ON l.lessontypeid = lesson_type.lessontypeid
ORDER BY
      [instructor name] ASC