画像を見てください。appointIDを持つappointTableに関連する5テーブルがあります。 今、私は同じ予定IDを持つすべてのデータが必要です。
これが生成されたクエリです(私はLeft outer Joinを使っています)
SELECT dbo.Appointment.appointment_id, dbo.Appointment.patient_id, dbo.PatientInvestigaiton.investigation_name, dbo.PatientInvestigaiton.investigation_id,
dbo.PatientTreatmentMedicine.medecine_id, dbo.PatientTreatmentMedicine.medicinename, dbo.PatientTreatmentMedicine.medicinetype,
dbo.PatientFindings.finding_id, dbo.PatientFindings.finding_value, dbo.PatientAdvice.advice_description, dbo.PatientCC.cc_value, dbo.PatientCC.cc_id,
dbo.PatientDiagonosis.diagonosis_name, dbo.PatientDiagonosis.diagonosis_id
FROM dbo.Appointment LEFT OUTER JOIN
dbo.PatientInvestigaiton ON dbo.Appointment.appointment_id = dbo.PatientInvestigaiton.appointment_id LEFT OUTER JOIN
dbo.PatientTreatmentMedicine ON dbo.Appointment.appointment_id = dbo.PatientTreatmentMedicine.appointment_id LEFT OUTER JOIN
dbo.PatientFindings ON dbo.Appointment.appointment_id = dbo.PatientFindings.appointment_id LEFT OUTER JOIN
dbo.PatientDiagonosis ON dbo.Appointment.appointment_id = dbo.PatientDiagonosis.appointment_id LEFT OUTER JOIN
dbo.PatientCC ON dbo.Appointment.appointment_id = dbo.PatientCC.appointment_id LEFT OUTER JOIN
dbo.PatientAdvice ON dbo.Appointment.appointment_id = dbo.PatientAdvice.appointment_id
where dbo.Appointment.appointment_id='46';
からappointmnent_id
の主キーです。Appointment
この表は1:N
6つのテーブルすべてとの関係。
これはこれらの6つのテーブルに結合することが作り出すケースです重複データを含む複数行、それはCartesian Product
。例えば、(1つだけの場合id=46
)、 がある:
PatientInvestigation
PatientTreatmentMedicine
PatientFindings
PatientDiagnosis
PatientCC
PatientAdvice
3×6×4×2×2×5 =1440
結果セットの行数、3 + 6 + 4 + 2 + 2 + 5(+1)=23
行それは必要とされるより60倍多くの行(そしてより多くの列を含む)です。
あなたがそうであればそれは良いです各クエリ内の1つの(6つの)テーブルに1つのJOINを持つ6つの別々のクエリ(基本表からデータを取得するためのもう1つの照会Appointment
)そして6つのクエリの結果をアプリケーションコードにまとめる。基本照会と最初の表に結合する照会の例
実テーブル:
SELECT
a.appointment_id,
a.patient_id
FROM
Appointment AS a
WHERE
a.appointment_id = 46
患者調査への参加-1:
SELECT
pi.investigation_name,
pi.investigation_id
FROM
Appointment AS a
JOIN
PatientInvestigation AS pi
ON pi.appointment_id = a.appointment_id
WHERE
a.appointment_id = 46
SELECT
Appointment.appointment_id,
Appointment.patient_id,
PatientInvestigaiton.investigation_name,
PatientInvestigaiton.investigation_id,
PatientTreatmentMedicine.medecine_id,
PatientTreatmentMedicine.medicinename,
PatientTreatmentMedicine.medicinetype,
PatientFindings.finding_id,
PatientFindings.finding_value,
PatientAdvice.advice_description,
PatientCC.cc_value,
PatientCC.cc_id,
PatientDiagonosis.diagonosis_name,
PatientDiagonosis.diagonosis_id
FROM
Appointment
LEFT OUTER JOIN PatientInvestigaiton ON Appointment.appointment_id = PatientInvestigaiton.appointment_id AND Appointment.appointment_id='46'
LEFT OUTER JOIN PatientTreatmentMedicine ON Appointment.appointment_id = PatientTreatmentMedicine.appointment_id
LEFT OUTER JOIN PatientFindings ON Appointment.appointment_id = PatientFindings.appointment_id
LEFT OUTER JOIN PatientDiagonosis ON Appointment.appointment_id = PatientDiagonosis.appointment_id
LEFT OUTER JOIN PatientCC ON Appointment.appointment_id = PatientCC.appointment_id
LEFT OUTER JOIN PatientAdvice ON Appointment.appointment_id = PatientAdvice.appointment_id
SELECT {TABLE1}.appointment_id,{OTHER FIELDS} FROM {TABLE1}
JOIN {TABLE2} ON {TABLE1}.appointment_id = {TABLE2}.appointment_id
JOIN {TABLE3} ON {TABLE1}.appointment_id = {TABLE3}.appointment_id
JOIN {TABLE4} ON {TABLE1}.appointment_id = {TABLE4}.appointment_id
JOIN {TABLE5} ON {TABLE1}.appointment_id = {TABLE5}.appointment_id
JOIN {TABLE6} ON {TABLE1}.appointment_id = {TABLE6}.appointment_id
JOIN {TABLE7} ON {TABLE1}.appointment_id = {TABLE7}.appointment_id
JOIN {TABLE8} ON {TABLE1}.appointment_id = {TABLE8}.appointment_id;
これを試して:
SELECT
dbo.Appointment.appointment_id, dbo.Appointment.patient_id,
dbo.PatientInvestigaiton.investigation_name, dbo.PatientInvestigaiton.investigation_id,
dbo.PatientTreatmentMedicine.medecine_id, dbo.PatientTreatmentMedicine.medicinename, dbo.PatientTreatmentMedicine.medicinetype,
dbo.PatientFindings.finding_id, dbo.PatientFindings.finding_value,
dbo.PatientAdvice.advice_description,
dbo.PatientCC.cc_value, dbo.PatientCC.cc_id,
dbo.PatientDiagonosis.diagonosis_name, dbo.PatientDiagonosis.diagonosis_id
FROM
dbo.Appointment
LEFT JOIN dbo.PatientInvestigaiton
ON dbo.Appointment.appointment_id = dbo.PatientInvestigaiton.appointment_id
LEFT JOIN dbo.PatientTreatmentMedicine
ON dbo.Appointment.appointment_id = dbo.PatientTreatmentMedicine.appointment_id
LEFT JOIN dbo.PatientFindings
ON dbo.Appointment.appointment_id = dbo.PatientFindings.appointment_id
LEFT JOIN dbo.PatientDiagonosis
ON dbo.Appointment.appointment_id = dbo.PatientDiagonosis.appointment_id
LEFT JOIN dbo.PatientCC
ON dbo.Appointment.appointment_id = dbo.PatientCC.appointment_id
LEFT JOIN dbo.PatientAdvice
ON dbo.Appointment.appointment_id = dbo.PatientAdvice.appointment_id
WHERE
dbo.Appointment.appointment_id='46';
JOIN
あるでしょう。 - Talha Ahmed Khan