gpt4 book ai didi

mysql - 如何内连接表?

转载 作者:行者123 更新时间:2023-11-29 03:01:17 24 4
gpt4 key购买 nike

我有 table

Patient_Detail id , name , check_in_id , check_out_id .

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表。

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

最佳答案

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

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

关于mysql - 如何内连接表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23276119/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com