SQL 内连接三表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14254597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inner Join Three Tables
提问by Usman YousafZai
I am working in Oracle APEX.i want to make a report from three tables through INNER JOIN
.The Tables are as Fallows.
我在 Oracle APEX 工作。我想从三个表到 .TheINNER JOIN
表作为休耕做一个报告。
PATIENT (Par_Id(Pk),Pat_Name,Pat_Gender)
HISTORY (His_Id(Pk),Pat_id(Fk),Treated_By)
患者 (Par_Id(Pk),Pat_Name,Pat_Gender)
历史(His_Id(Pk),Pat_id(Fk),Treated_By)
and
和
Treatment ( Treat_Id, His_id(Fk),Pat_id(Fk) ,Treat_Type ,Charges)
治疗 ( Treat_Id, His_id(Fk), Pat_id(Fk) ,Treat_Type , Charges)
I want to display all the columns in Report mentioned in the above three Tables.
我想显示上面三个表中提到的Report中的所有列。
Thanks.
谢谢。
回答by Rhumborl
You should always specify the columns to return, especially as the tables contain identical column names
您应该始终指定要返回的列,尤其是当表包含相同的列名时
SELECT p.Par_Id, p.Pat_Name, p.Pat_Gender,
h.His_Id, h.Treated_By,
t.Treat_Id, t.Treat_Type, t.Charges
FROM Patient p
INNER JOIN History h
ON p.PAR_ID = h.PAT_ID
INNER JOIN Treatment t
ON h.HIS_ID = t.HIS_ID AND p.PAR_ID = h.PAT_ID
回答by Tikkes
This should do the trick
这应该可以解决问题
SELECT * FROM Patient p
INNER JOIN History h
ON p.PAR_ID = h.PAT_ID
INNER JOIN Treatment t
ON h.HIS_ID = t.HIS_ID AND p.PAR_ID = h.PAT_ID
回答by Muhammad Raheel
It is too simple in mysql
在mysql中太简单了
SELECT
*
FROM PATIENT as p
LEFT JOIN HISTORY as h ON h.Pat_id = p.Pat_Id
LEFT JOIN Treatment as t ON t.His_id = h.His_Id
回答by Pranay Rana
Try this
尝试这个
Select * from
PATIENT inner join HISTORY on par_id=HISTORY.Pat_id
inner join Treatment on par_id=Treatment.Pat_id