2013-10-16 72 views
1

請幫忙寫查詢。MySQL ManyToMany顯示重複行

我有三個表:

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

查找患者姓氏DOB一個內PhysicianOrganizationId是相似的。

我會告訴你,你更好地理解這個問題的數據:

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

嘗試增加pt.IdGROUP BY條款:

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; 

注:我還沒有測試過上述SQL

+0

我試過這個查詢沒有顯示任何行。 「空集(0.00秒)」 –