1

我有一个关于 SQL 语句的问题,由于某种原因,我的脚本无法正常工作。这是场景;我有 6 张桌子

Patient
-PatientID
-PatientName
-EmployerID FK to employer.EmployerID.

Employer
-EmployerID
-EmployerName

Carrier
-CarrierID
-CarrierName

Appointment
-AppointmentID
-AptDateTime
-PatientID FK to patient.PatientID

InsurancePlan
-PlanID
-GroupName
-EmployerID FK to employer.EmployerID
-CarrierID FK to carrier.CarrierID

Inssub
-InsubID
-DateEffective
-PlanID FK to insplan.PlanID
-Suscriber FK to patient.PatientID

我的脚本:我需要从这 5 个表中获取所有行。我不太擅长 SQL 索引验证,这就是我的脚本无法正常工作的原因!

SELECT p.PatientName, e.EmployerName, c.CarrierName, ip.GroupName, a.AptDateTime, i.DateEffective

FROM patient p, employer e, inssub i, InsurancePlan ip, carrier c, appointment a

WHERE e.EmployerNum = p.EmployerNum AND 
      i.Subscriber = p.PatientID AND
      i.PlanID = ip.PlanID AND
      ip.CarrierID = c.CarrierID AND
      ip.employerID = e.EmployerID AND
      ip.PlanID = i.PlanID AND
          a.PatientID = p.PatientID AND
          a.DateTStamp > '2013/01/01' AND    
      a.AptDateTime != '0001-01-01 00:00:00'
4

2 回答 2

3

如果您使用显式 JOINS 而不是逗号分隔的表,它会更简单:

SELECT p.PatientName, e.EmployerName, c.CarrierName, ip.GroupName, a.AptDateTime, i.DateEffective
FROM patient p
JOIN employer e ON p.EmployerID = e.EmployerID
JOIN insuranceplan ip ON e.EmployerID = ip.EmployerID
JOIN carrier c ON ip.CarrierID = c.CarrierID
JOIN appointment a ON p.PatientID = a.PatientID
JOIN inssub i ON p.PatientID = i.Subscriber AND ip.PlanID = i.PlanID
WHERE a.DateTStamp > '2013/01/01'
  AND a.AptDateTime != '0001-01-01 00:00:00'
于 2013-05-21T03:08:51.847 回答
0
SELECT p.PatientName, e.EmployerName, c.CarrierName, ip.GroupName, a.AptDateTime
FROM Patient p JOIN Employer e ON p.EmployerId=e.EmployerId
JOIN Appointment a ON p.PatientId= a.PatientId
JOIN InsurancePlan ip ON ip.EmployerId=e.EmployerId
JOIN Carrier c ON c.CarrierId=ip.CarrierId
Join Inssub i ON i.PlanId=ip.PlanId
WHERE a.DateTStamp > '2013/01/01'
AND a.AptDateTime != '0001-01-01 00:00:00'

试试这个看看

于 2013-05-21T03:17:57.363 回答