左连接查询显示重复结果的问题

时间:2014-03-08 05:34:01

标签: mysql sql

我有表personcourses_by_instructors。我正在执行一个SQL查询,但我得到了所需结果的重复值。我正在从person表中获取当前正在教授instructor_roles = 1的所有course人。可以对查询或表格提出哪些建议? SQLFIDDLE

SELECT p.person_id, p.person_name, p.instructor_role, IFNULL(lj.company_id, 0) as  company_id, lj.person_id, lj.course_name
FROM person as p
LEFT JOIN courses_by_instructors as lj
      ON (lj.person_id = p.person_id AND lj.company_id = 7 )
WHERE instructor_role = 1
ORDER BY person_name

期望的结果:

+-----------+-----------------+-------------+------------+
| person_id | instructor_name | course_name | company_id |
+-----------+-----------------+-------------+------------+
|       154 | James Newton    |             |            |
|       154 | James Newton    |             |            |
|       478 | Tina Turner     |             |            |
|       258 | James Fuller    |             |            |
|       717 | Michael Jordan  | Shoe Biz    |          7 |
|       717 | Michael Jordan  | Basketball  |          7 |
|       964 | Steve Johnson   |             |            |
+-----------+-----------------+-------------+------------+

电流:

+-----------+-----------------+-------------+------------+
| person_id | instructor_name | course_name | company_id |
+-----------+-----------------+-------------+------------+
|       154 | James Newton    |             |            |
|       154 | James Newton    |             |            |
|       478 | Tina Turner     |             |            |
|       258 | James Fuller    |             |            |
|       717 | Michael Jordan  | Shoe Biz    |          7 |
|       717 | Michael Jordan  | Basketball  |          7 |
|       717 | Michael Jordan  | Shoe Biz    |          7 |
|       717 | Michael Jordan  | Basketball  |          7 |
|       964 | Steve Johnson   |             |            |
+-----------+-----------------+-------------+------------+

5 个答案:

答案 0 :(得分:1)

查询没问题,但问题出在您的模型中。 我会说人员表应该像这样创建:

CREATE TABLE person
    (
     company_id int, 
     person_id int, 
     person_name varchar(30),
     instructor_role tinyint,
     staff_role tinyint,
     CONSTRAINT PK_PERSON_ID PRIMARY KEY NONCLUSTERED (person_id), 
    );

将人员ID作为主键。 course_by_instructors表应该像这样创建:

CREATE TABLE courses_by_instructors
    (
     company_id int, 
     person_id int, 
     course_id int,
     course_name varchar(30),
     CONSTRAINT FK_PERSON_ID FOREIGN KEY (person_id) 
        REFERENCES person(person_id) 
        ON DELETE CASCADE
        ON UPDATE CASCADE
    );

使用外键引用人员ID。

请注意我正在使用SQL Server,因此您可能需要稍微调整一下语法。

答案 1 :(得分:1)

试试这个:

SELECT p.person_id, p.person_name, p.instructor_role, IFNULL(lj.company_id, 0) as  company_id, lj.person_id, lj.course_name
FROM person as p
LEFT JOIN courses_by_instructors as lj
      ON (lj.person_id = p.person_id AND lj.company_id = 7 )
WHERE instructor_role = 1
GROUP BY p.person_id,lj.course_name
ORDER BY person_name

它会为您提供所需的信息。

请参阅SQL Fiddle

中的输出

答案 2 :(得分:0)

SELECT p.person_id, p.person_name, p.instructor_role, IFNULL(lj.company_id, 0) as  company_id, lj.person_id, lj.course_name
FROM person as p
LEFT JOIN courses_by_instructors as lj
  ON (lj.person_id = p.person_id AND lj.company_id = p.company_id )
WHERE instructor_role = 1
ORDER BY person_name

答案 3 :(得分:0)

只是得到你的要求:

SELECT * from person
left join (select * from courses_by_instructors group by company_id) as mytable on mytable.person_id = person.person_id
where instructor_role = 1
order by person.person_name

但是,您的初始查询中没有重复项,因为company_id会有所不同

答案 4 :(得分:0)

它可以通过SELECT语句中的DISTINCT实现

SELECT DISTINCT p.person_id, p.person_name, p.instructor_role, IFNULL(lj.company_id, 0) as  company_id, lj.person_id, lj.course_name
FROM person as p
LEFT JOIN courses_by_instructors as lj
      ON (lj.person_id = p.person_id AND lj.company_id = 7 )
WHERE instructor_role = 1
ORDER BY person_name

以下是SQL Fiddle

的代码