SQL显式连接的过滤条件
问题描述:
使用这种关系模式,病人ID和工作人员ID与id.staff
和pid.patient
是唯一键外键:SQL显式连接的过滤条件
staff(ID, fname, lname, role)
patient(pID, pFname, pLname, bdate, address, phone)
appointment(aptID, patientID, staffID, aptDate, aptTime)
procedures(procNum, pName, price)
aptDetail(aptID, procNo)
所以说,如果我要列举的病人的姓名与预约一个特定的工作人员,即约翰史密斯,我将如何明确地做到这一点?
我已隐式管理,但我知道这是有点皱眉,但我不能使用WHERE
语句达到它。
任何帮助将不胜感激,任何时候我尝试和使用INNER JOIN
如果不是简单的连接,我似乎打了一堵墙。
答
这是你要找的查询的类型?
select distinct pFname, pLname
from patient p
join appointment a on p.pID = a.patientID
join staff s on a.staffID = s.ID
where s.fname = 'John' and s.lname = 'Smith'
答
您可以使用内部联接
select
a.pID
, a.pFname
, a.pLname
, a.bdate
, a.address
, a.phone
, b.aptDate
, b.aptTime
, c.fname
, c.lname
, c.role
from patient a
INNER JOIN appointment b on b.patientID = a.pID
INNER JOIN staff c on b.staffID = c.ID on concat(fname, ' ', lname) ='John Smith'
答
类似下面应该很好地工作:
SELECT p.*
FROM appointment AS a
INNER JOIN staff AS s ON a.staffID = s.pID
INNER JOIN patient AS p ON a.patientID = p.pID
WHERE s.ID = <yourstaffid>;
答
select staff.fname, staff.role,
patient.pfname, plname, appoitment.aotdate
from staff, patient, appointment
where patient.pid=appointment.patientId
and staff.id=appointment.staffid