私は技術者のリストと患者数を持っています。私が技術者をクリックすると、患者のリストとその詳細が表示されます。 これを行うには、技術者1人あたりの患者数を返すクエリと、患者のレコードを返すクエリがあります。ユニオンクエリーからレコード数の違いを解決する方法
SELECT *, SUM(Rcount) as Number_of_patients
FROM
(
SELECT users.users_id, users.name, patients.patients_id, count(*) as Rcount
FROM pecs
INNER JOIN users ON pecs.techniciens_id = users.users_id
INNER JOIN titles ON users.titles_id = titles.titles_id
INNER JOIN patients ON patients.patients_id = pecs.patients_id
GROUP BY users_id
UNION ALL
SELECT users.users_id, users.name, patients.patients_id, count(*) as Rcount
FROM followup
INNER JOIN users ON followup.technician_id = users.users_id
INNER JOIN titles ON users.titles_id = titles.titles_id
INNER JOIN pecs ON pecs.pecs_id = followup.pecs_id
INNER JOIN patients ON patients.patients_id = pecs.patients_id
GROUP BY users_id
)x
GROUP BY users_id ORDER BY last_name ASC
結果は次のとおりです。
users_id | name | Number_of_patients
40 | ABABAB | 223
この技術者のための患者のリストを表示するには、今すぐ= 40
technician_IDための223人の患者である私は、次のクエリがあります
をSELECT *
FROM
(
SELECT patients.patients_id, patients.name
FROM pecs
LEFT JOIN users ON pecs.techniciens_id = users.users_id
LEFT JOIN titles ON users.titles_id = titles.titles_id
INNER JOIN patients ON patients.patients_id = pecs.patients_id
WHERE pecs.techniciens_id = 40
#GROUP BY patients_id
UNION ALL
SELECT patients.patients_id, patients.name
FROM followup
LEFT JOIN users ON followup.technician_id = users.users_id
LEFT JOIN titles ON users.titles_id = titles.titles_id
LEFT JOIN pecs ON pecs.pecs_id = followup.pecs_id
INNER JOIN patients ON patients.patients_id = pecs.patients_id
WHERE followup.technician_id = 40
#GROUP BY patients_id
)x
GROUP BY patients_id ORDER BY last_name ASC
今、私は同じ数のレコードを取得しますが(223)、重複した患者の行があります...私はhが必要です重複することなく各技術者の患者数を正確に取得する方法についてのELP。
誰でもお手伝いできますか?
ようこそスタックオーバーフロー。あなたの質問は読みにくいです。書式を修正してください。また、おそらくこれは多くの情報が答えを得る必要はありません。あなたの質問を最小限のサンプルに収めてください。 –