如何内连接表?

时间:2014-04-24 17:43:53

标签: mysql sql sql-server

我有桌子
具有 id,name,check_in_id,check_out_id Patient_Detail
{Check_Out Check_Out_ID,Illness_ID,Check_Out_Date。

Check_In Check_In_ID,Illness_ID,Check_In_Date。

Illness Illness_ID,Illness_Name。

但问题是我不知道如何通过illness_Name表加入Check_out。< / p>
USE PantienDatabase

SELECT Name,Check_in,Illness_name,Check_out_ID FROM Check_In AS CI
INNER JOIN Patient_Detail AS P 
ON CI.Check_In_ID = P.Check_In_ID 
INNER JOIN Illness AS I 
ON I.IllnessID  =CI.illness_ID
INNER JOIN 
Check_Out AS CO
ON CO.Check_Out_ID = P.Check_out_ID

1 个答案:

答案 0 :(得分:1)

您可以将同一个表连接两次,并为所选列使用别名:

SELECT Name, Check_in,
       II.Illness_name AS Illness_name_in,
       IO.Illness_name AS Illness_name_out,
       Check_out_ID
FROM Check_In AS CI
INNER JOIN Patient_Detail AS P ON CI.Check_In_ID = P.Check_In_ID 
INNER JOIN Illness AS II ON II.IllnessID = CI.illness_ID
INNER JOIN Check_Out AS CO ON CO.Check_Out_ID = P.Check_out_ID
INNER JOIN Illness AS IO ON IO.IllnessID = CO.illness_ID