MySQL-如何从多个表中选择多行
我有一个关于SQL语句的问题,由于某些原因,我的脚本无法正常工作。。 情况就是这样;我有6张桌子MySQL-如何从多个表中选择多行,mysql,rows,Mysql,Rows,我有一个关于SQL语句的问题,由于某些原因,我的脚本无法正常工作。。 情况就是这样;我有6张桌子 Patient -PatientID -PatientName -EmployerID FK to employer.EmployerID. Employer -EmployerID -EmployerName Carrier -CarrierID -CarrierName Appointment -AppointmentID -AptDateTime -PatientID FK to pat
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'
如果使用显式联接而不是逗号分隔的表,则会更简单:
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'
请尝试此操作,然后查看缺少的
employer
join。对于所使用的SELECT,实际上不需要inssub
。你是在补充我遗漏的东西吗?我在第一份声明中就包括了雇主本身。遗漏了-我道歉。但是这里不需要inssub
表。没问题。它可能看不清楚,因为它把它放在一起了。很抱歉,是我的错误,我错过了inssub的DateEffective on select语句(已更新)。我尝试了此脚本,但仍然没有获得数据库中的所有记录,。我一共有3个,刚刚回来2@Jay,更新以注意附加列。如果这不能满足您的需要,您能否发布一些示例数据,以及您希望看到的结果?
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'