2017-08-07 1 views
0

Mit diesem relationalen Schema, Patienten-ID und Mitarbeiter ID sind Fremdschlüssel mit id.staff und pid.patient sind eindeutige Schlüssel:SQL explizite Join-Filterbedingung

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) 

So sagen, wenn ich die Namen von Patienten mit Terminen zur Liste wollte mit ein bestimmter Mitarbeiter, dh John Smith, wie würde ich das explizit machen?

Ich habe implizit verwaltet, aber ich weiß, das ist irgendwie verpönt, aber ich kann es nicht erreichen, ohne WHERE Aussagen zu verwenden.

Jede Hilfe würde geschätzt werden, jedes Mal, wenn ich versuche und INNER JOIN benutze ich scheine eine Wand zu schlagen, wenn es keine einfache Verbindung ist.

Antwort

2

Ist dies die Art der Abfrage, die Sie suchen?

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' 
1

können Sie Innen verwenden

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' 
1

So etwas wie die folgenden beitreten sollte gut funktionieren:

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>; 
0
select staff.fname, staff.role, 
patient.pfname, plname, appoitment.aotdate 
from staff, patient, appointment 
where patient.pid=appointment.patientId 
and staff.id=appointment.staffid