이미지를보십시오. appointID가있는 appointTable과 관련된 5 개의 테이블이 있습니다. 이제 같은 약속 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 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
. 예를 들어 (id=46
),있다 :
PatientInvestigation
PatientTreatmentMedicine
PatientFindings
PatientDiagnosis
PatientCC
PatientAdvice
당신은 3x6x4x2x2x5 =1440
결과 집합의 행은 3 + 6 + 4 + 2 + 2 + 5 (+1) =23
행. 필요한 것보다 60 배 많은 행 (열이 더 많음)입니다.
니가한다면 더 좋다.각각의 쿼리에서 하나의 테이블 (6 개 테이블)에 하나의 JOIN을 갖는 6 개의 개별 쿼리(그리고 기본 테이블에서 데이터를 얻기위한 하나 이상의 쿼리Appointment
). 과응용 프로그램 코드에서 6 개의 쿼리 결과 결합. 기본 쿼리와 첫 번째 테이블에 조인 할 쿼리의 예 :
기본 테이블:
SELECT
a.appointment_id,
a.patient_id
FROM
Appointment AS a
WHERE
a.appointment_id = 46
PatientInvestigation에 가입 -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