2013-10-16 2 views
1

도움말을 작성하는 데 도움이됩니다.MySQL ManyToMany 중복 행 표시

+-------------------+ 
| Patient   | 
| PatientPhysician | 
| Physician   | 
+-------------------+ 

환자 그의 FIRSTNAME,성 하나 내 DOBPhysicianOrganizationId이 비슷 찾기 :

나는 세 개의 테이블이있다.

나는 당신에게 당신이 더 문제를 이해하고 데이터가 표시됩니다 :

mysql> SELECT pt.Id, pt.FirstName, pt.LastName, pt.DoB, ph.PhysicianOrganizationId 
     -> FROM Patient pt, Physician ph, PatientPhysician pp 
     -> WHERE pt.Id = pp.IdPatient AND ph.Id = pp.IdPhysician 
     -> ORDER BY pt.Id; 

+----+-----------+-------------+------------+-------------------------+ 
| Id | FirstName | LastName | DoB  | PhysicianOrganizationId | 
+----+-----------+-------------+------------+-------------------------+ 
| 1 | Mario  | Gotze  | 1989-01-09 |      101 | 
| 2 | Mario  | Gotze  | 1989-01-09 |      102 | 
| 3 | Mario  | Gotze  | 1989-01-09 |      101 | 
| 4 | Fillip | Gotze  | 1989-01-09 |      101 | 
| 5 | Marco  | Rues  | 1988-09-12 |      102 | 
| 5 | Marco  | Rues  | 1988-09-12 |      101 | 
| 5 | Marco  | Rues  | 1988-09-12 |      103 | 
| 6 | Dimitri | Payet  | 1986-10-10 |      101 | 
| 7 | Dimitri | Payet  | 1986-10-10 |      101 | 
| 8 | Dimitri | Payet  | 1986-10-10 |      101 | 
| 8 | Dimitri | Payet  | 1986-10-10 |      102 | 
| 9 | Zlatan | Ibrahimovic | 1982-01-12 |      103 | 
| 9 | Zlatan | Ibrahimovic | 1982-01-12 |      101 | 
| 10 | Zlatan | Ibrahimovic | 1982-01-12 |      101 | 
| 10 | Zlatan | Ibrahimovic | 1982-01-12 |      103 | 
+----+-----------+-------------+------------+-------------------------+ 
15 rows in set (0.01 sec) 

내가 쿼리를 작성을하지만 잘못된 결과를 생성합니다

SELECT 
    pt.Id, 
    pt.FirstName, 
    pt.LastName, 
    pt.DoB, 
    ph.PhysicianOrganizationId 

FROM Patient pt, Physician ph, PatientPhysician pp 

WHERE pt.Id = pp.IdPatient AND ph.Id = pp.IdPhysician 

GROUP BY pt.FirstName, pt.LastName, pt.DoB, ph.PhysicianOrganizationId 

HAVING COUNT(*) > 1 ORDER BY pt.Id; 

결과 :

+----+-----------+-------------+------------+-------------------------+ 
| Id | FirstName | LastName | DoB  | PhysicianOrganizationId | 
+----+-----------+-------------+------------+-------------------------+ 
| 1 | Mario  | Gotze  | 1989-01-09 |      101 | 
| 6 | Dimitri | Payet  | 1986-10-10 |      101 | 
| 9 | Zlatan | Ibrahimovic | 1982-01-12 |      103 | 
| 9 | Zlatan | Ibrahimovic | 1982-01-12 |      101 | 
+----+-----------+-------------+------------+-------------------------+ 

을 이 결과가 필요합니다.

+----+-----------+-------------+------------+-------------------------+ 
| Id | FirstName | LastName | DoB  | PhysicianOrganizationId | 
+----+-----------+-------------+------------+-------------------------+ 
| 1 | Mario  | Gotze  | 1989-01-09 |      101 | 
| 3 | Mario  | Gotze  | 1989-01-09 |      101 | 
| 6 | Dimitri | Payet  | 1986-10-10 |      101 | 
| 7 | Dimitri | Payet  | 1986-10-10 |      101 | 
| 8 | Dimitri | Payet  | 1986-10-10 |      101 | 
| 9 | Zlatan | Ibrahimovic | 1982-01-12 |      103 | 
| 9 | Zlatan | Ibrahimovic | 1982-01-12 |      101 | 
| 10 | Zlatan | Ibrahimovic | 1982-01-12 |      103 | 
| 10 | Zlatan | Ibrahimovic | 1982-01-12 |      101 | 
+----+-----------+-------------+------------+-------------------------+ 

내가 뭘 잘못하고 있는지 말해?

답변

1
SELECT pt.Id, tmp1.fname, tmp1.lname, tmp1.dob, tmp1.poid 

FROM (

    SELECT pt.FirstName AS fname, 
     pt.LastName AS lname, 
     pt.DoB as dob, 
     ph.PhysicianOrganizationId AS poid 

    FROM Patient pt, Physician ph, PatientPhysician pp 

    WHERE pt.Id = pp.IdPatient AND ph.Id = pp.IdPhysician 

    GROUP BY fname, lname, dob, poid 

    HAVING COUNT(*) > 1) AS tmp1 

JOIN Patient AS pt ON pt.FirstName = tmp1.fname AND pt.LastName = tmp1.lname AND pt.DoB = tmp1.dob 

JOIN PatientPhysician AS pp ON pt.Id = pp.IdPatient 

JOIN Physician AS ph ON ph.Id = pp.IdPhysician AND tmp1.poid = ph.PhysicianOrganizationId 

ORDER BY pt.Id; 
+0

좋습니다! 고마워요/ –

+0

작은 실수를 할 때마다 바꿀 것입니다. –

0

당신의 GROUP BY 절에 pt.Id를 추가하는 시도 :

SELECT 
    pt.Id, 
    pt.FirstName, 
    pt.LastName, 
    pt.DoB, 
    ph.PhysicianOrganizationId 
FROM 
    Patient pt, 
    Physician ph, 
    PatientPhysician pp 
WHERE 
    pt.Id = pp.IdPatient 
AND ph.Id = pp.IdPhysician 
GROUP BY 
    pt.Id, 
    pt.FirstName, 
    pt.LastName, 
    pt.DoB, 
    ph.PhysicianOrganizationId 
HAVING COUNT(*) > 1 
ORDER BY pt.Id; 

N.B. 위의 SQL을 테스트하지 않았습니다

+0

이 쿼리에서는 행이 표시되지 않았습니다. "빈 세트 (0.00 초)" –