mysql-连接具有相互良好引用的4个表

前端之家收集整理的这篇文章主要介绍了mysql-连接具有相互良好引用的4个表 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我对这个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

人物

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
最佳答案
为了获得访问或意外的结果,您需要将这两个表联合在一起,为该表中没有对应数据的列选择NULL值(例如,访问中的acc_id).然后可以将这些结果加入到医生和人员表中,以获取每次访问/意外的相关医生和人员信息:

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,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

原文链接:https://www.f2er.com/mysql/531900.html

猜你在找的MySQL相关文章