使用公用表在三个表上SQL JOIN

时间:2015-05-28 06:40:31

标签: c# sql asp.net

我必须在3个三桌上进行连接,方案如下:

 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...

现在我想进行连接,我可以从Donor中选择一些列,来自Request的一些列。那怎么能这样呢? 我目前的疑问是:

   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"];

请提前帮助并表示感谢。

4 个答案:

答案 0 :(得分:1)

您的问题是Donor.DonorID=Request_Send.DonorID

尝试此查询您的问题已解决。

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"] + "'";

答案 1 :(得分:1)

您需要提供talbe name,其中D_Emial将选择

等数据
Where Request_Send.D_Emial ='something';

答案 2 :(得分:0)

Donor.DonorID不存在。在您的加入中尝试Donor.ID

答案 3 :(得分:0)

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。