连接4个表,彼此之间有很好的引用

时间:2019-01-07 22:42:39

标签: mysql join

我对这个MYSQL查询视而不见。这不应该那么难,但我确实可以取得结果,但不是我想要的结果。 非常感谢您的帮助!

医生

dctr_id | dctr_name | ...
--------------------------
60      | Bezant

访问

vist_id | dctr_id| prsnl_id | visit_date | ...
-----------------------------------------------
1       | 60      | 86      | 2018-12-31

事故

acc_id | dctr_id | prsnl_id| acc_date | ...
--------------------------------------------
51     | 60      | 86      | 2018-12-25
55     | 60      | 86      | 2018-12-20

personell

prsnl_id | prsnl_name | ...
---------------------------
79       | test_name2
86       | test_name

我尝试了各种查询,但没有一个能解决问题。独特,分组...

我得到这个结果:

dctr_id | dctr_name | visit_id | visit_date | acc_id | acc_date | prsnl_id | prsnl_name
-----------------------------------------------------------------------------------------
60      | Bezant    | 1        | 2018-12-31 | 51     | 2018-12-25 | 79     | test_name2
60      | Bezant    | 1        | 2018-12-31 | 51     | 2018-12-25 | 79     | test_name2
60      | Bezant    | 1        | 2018-12-31 | 55     | 2018-12-20 | 86     | test_name1


SELECT DISTINCT dctr.dctr_id 
              , dctr.dctr_name 
              , vst.visit_id
              , vst.visit_date
              , acc.acc_id
              , acc.acc_date,prsnl.prsnl_id
              , prsnl.name  
          FROM doctor dctr
          LEFT 
          JOIN visits vst 
           ON vst.dctr_id = dctr.dctr_id
          LEFT 
          JOIN accidents acc 
           ON acc.dctr_id = dctr.dctr_id
          LEFT 
          JOIN personell prsnl 
            ON prsnl.prsnl_id = vst.prsnl_id 
            OR prsnl.prsnl_id = acc.prsnl_id
         WHERE dctr.dctr_id = 60

我想得到以下结果:

dctr_id | dctr_name | visit_id | visit_date | acc_id | acc_date | prsnl_id | prsnl_name
-----------------------------------------------------------------------------------------
60      | Bezant    | 1        | 2018-12-31 | NULL   | NULL       | 79     | test_name2
60      | Bezant    | NULL     | NULL       | 51     | 2018-12-25 | 79     | test_name2
60      | Bezant    | NULL     | NULL       | 55     | 2018-12-20 | 86     | test_name1

2 个答案:

答案 0 :(得分:1)

您需要两个带有联合的SQL查询。检查我的答案:

SELECT 
  dctr.dctr_id ,
  dctr.dctr_name ,
  null,
  null,
  acc.acc_id,
  acc.acc_date,
  prsnl.prsnl_id,
  prsnl.prsnl_name  
FROM doctor dctr
LEFT JOIN accidents acc ON acc.dctr_id = dctr.dctr_id
LEFT JOIN personell prsnl ON prsnl.prsnl_id = acc.prsnl_id
WHERE dctr.dctr_id = '60'
union
SELECT 
  dctr.dctr_id ,
  dctr.dctr_name ,
  vst.vist_id,
  vst.visit_date,
  null,null,
  prsnl.prsnl_id,
  prsnl.prsnl_name 
FROM doctor dctr
LEFT JOIN visits vst ON vst.dctr_id = dctr.dctr_id
LEFT JOIN personell prsnl ON prsnl.prsnl_id = vst.prsnl_id
WHERE dctr.dctr_id = '60';

这里是Fiddle Link

PS 我在Fiddle中使用了Oracle DB,但是SQL使用ANSI语法。不用担心。

答案 1 :(得分:1)

要从visitsaccidents获得结果,您需要将UNION这两个表放在一起,为其中没有对应数据的列选择NULL值表(例如acc_id中的visits)。然后可以将这些结果JOIN放入doctorpersonell表中,以获取每次访问/意外的相关医生和人员信息:

SELECT d.dctr_id ,d.dctr_name ,i.visit_id,i.visit_date,i.acc_id,i.acc_date,p.prsnl_id,p.prsnl_name
FROM doctor d
LEFT JOIN (SELECT dctr_id, visit_id, prsnl_id, visit_date, NULL AS acc_id, NULL AS acc_date
           FROM visits
           UNION
           SELECT dctr_id, NULL, prsnl_id, NULL, acc_id, acc_date
           FROM accidents) i
  ON i.dctr_id = d.dctr_id
LEFT JOIN personell p ON p.prsnl_id = i.prsnl_id
WHERE d.dctr_id = 60
ORDER BY i.visit_id, i.acc_id

输出:

dctr_id     dctr_name   visit_id    visit_date  acc_id  acc_date    prsnl_id    prsnl_name
60          Bezant      1           2018-12-31  (null)  (null)      79          test_name2
60          Bezant      (null)      (null)      51      2018-12-25  79          test_name2
60          Bezant      (null)      (null)      55      2018-12-20  86          test_name

Demo on SQLFiddle