简体   繁体   中英

SQL JOIN on three tables using a common table

I have to make a join on 3 three table, the scheme is following:

 1. Request_Send table have: RequestID(FK),DonorID(FK),SendRequestID(PK)

 2. Donor table have Donor,ID(PK),Name,etc...

 3. Blood_Request table have RequestID(PK),etc...

Now I want to make join in which I could select some columns from Donor, some columns from Request. so how could I do this? my present query is:

   string show = "
SELECT Blood_Request.Date,Blood_Request.Time,Blood_Request.R_Name,R_Address,R_Phone 
FROM    Request_Send INNER JOIN 
        Blood_Request ON Blood_Request.RequestID=Request_Send.RequestID INNER JOIN 
        Donor ON Request_Send.DonorID=Donor.DonorID
Where D_Emial='" + Session["UserID"];

Please Help and thanks in advance.

Your problem is Donor.DonorID=Request_Send.DonorID

try this query your problem is solved.

string show ="
SELECT  Blood_Request.Date,Blood_Request.Time,Blood_Request.R_Name,R_Address,R_Phone 
FROM    Request_Send INNER JOIN 
        Blood_Request ON Blood_Request.RequestID=Request_Send.RequestID INNER JOIN 
        Donor ON Donor.DonorID=Request_Send.DonorID 
WHERE D_Emial='" + Session["UserID"] + "'";

您需要提供D_Emial talbe nameD_Emial将从那里选择数据,例如

Where Request_Send.D_Emial ='something';

Donor.DonorID doesn't exist. Try Donor.ID in your join

string show = "select bld_req.Date,bld_req.Time,bld_req.R_Name,R_Address,R_Phone FROM Request_Send req_snd INNER JOIN Blood_Request bld_req ON bld_req.RequestID=req_snd.RequestID INNER JOIN Donor dnr ON req_snd.DonorID=dnr.ID Where D_Emial='" + Session["UserID"] + "'";

请确保表中有D.Emial列,并按如下方式调用它:tablename.D_Emial。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM