多个表中的多个连接

时间:2013-02-27 14:36:17

标签: sql inner-join multiple-tables

我有下表:

patient: id, incidentCode, name, insurance, contactdetailsID
contactDetails: id, cityCode
lookup: id, lookupdescription
incident: id, date

患者加入联系方式,如:

inner join contactdetails on patient.contactdetailsid=contactdetails.id
患者加入了如下事件:

inner join incident on patient.incidentCode=incident.id

患者加入查找,如:

inner join lookup on patient.insurance = lookup.id

和联系人详细信息加入查找,如:

inner join lookup on contactdetails.citycode = lookup.id

现在我想从患者保险和contactdetails Citycode中选择Lookup.lookupDescription。 我怎样才能做到这一点?在选择我也想要patient.name,patient.id,incident.date

2 个答案:

答案 0 :(得分:1)

例如

SELECT
  patient.id,
  lookup.lookupdescription,
  contactDetails.cityCode
FROM patient
INNER JOIN contactdetails on patient.contactdetailsid=contactdetails.id
INNER JOIN ...
INNER JOIN ...
INNER JOIN ...
WHERE patient.id = xy

顺便说一句:你提供的最后一个JOIN对我来说看起来不那么有意义?!看起来lookup.id是城市代码,同时也是保险号码?!

答案 1 :(得分:0)

SELECT  d.lookupDescription,
        a.insurance,
        e.Citycode,
        a.name,
        a.id,
        c.date
FROM    patient a
        INNER JOIN contactdetails b
            ON a.contactdetailsid = b.id
        INNER JOIN  incident c
            ON a.incidentCode = c.id
        INNER JOIN lookup d
            ON a.insurance = d.id
        INNER JOIN contactdetails e
            ON e.citycode = d.id

要进一步了解联接,请访问以下链接: