クエリの作成を手伝ってください。
私は3つのテーブルを持っています:
+-------------------+
| Patient |
| PatientPhysician |
| Physician |
+-------------------+
PhysicianOrganizationId内のFirstName、LastName、およびDoBが類似している患者を検索します。
問題をよりよく理解するためのデータを示します。
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 |
+----+-----------+-------------+------------+-------------------------+
私が間違っていることを教えてください。