我编写此SQL查询是为了获得特定患者的剩余收益,由于患者ID是唯一的,因此期望它返回一行,但它会不断返回多行。我该如何解决? DBMS是MySql
select Plan.pBenefitMax - sum(Transaction.employerTotal) as RemainingBenefit
from Patient
inner join Plan on Patient.pPlanId = Plan.planId
inner join Transaction on Patient.patientId = Transaction.tPatientId
where Patient.patientId = 1
Group by Plan.pBenefitMax;
答案 0 :(得分:1)
如果没有得到一行,则说明您有重复的PatientId值,或者PatientId已链接到多个计划。
SELECT Plan.pBenefitMax -
(SELECT SUM(`Transaction`.employerTotal)
FROM `Transaction`
WHERE `Transaction`.tPatientId = Patient.patientId)
AS RemainingBenefit
FROM Patient
JOIN Plan ON Patient.pPlanId = Plan.planId
WHERE Patient.patientId = 1;