跨多个表获取值(包括 MAX 和 NULL)

发布于 2024-10-17 05:06:07 字数 1360 浏览 1 评论 0原文

因此,我将尝试解释数据库设置

tblEDS

eID | eVal1     | eVal2
-------------------------------------
1   | Monday    | Joe
2   | Tuesday   | Bob
3   | Wednesday | Jan
4   | Thursday  | Pete

tblPatient(我意识到这不是最好的数据库设计!)

pID | pVal1     | pVal2 | pEDS
------------------------------
1   | Monday    | 123   | 1
2   | Tuesday   | 456   | 2

tblPatientRecords

pID | rID
------------
1   | 1
1   | 2
2   | 3
2   | 4

tblRecords

rID | rVal1 | rVal2
-------------------
1   | Ok    | Boy
2   | Well  | Man
3   | Dead  | Kid
4   | Dead  | Girl

我需要的是一个查询来拉回 tblAlert 中的所有记录,无论它在 tblPatient 中是否有相应的值。对于 tblPatient 中存在的每条记录,我需要返回相关的最大 rID 和相关值,即

eID | eVal1     | eVal2 | pID  | pVal1   | pVal2 | rID  | rVal1  | rVal2     
-------------------------------------------------------------------------
1   | Monday    | Joe   | 1    | Monday  | 123   | 2    | Well   | Man
2   | Tuesday   | Bob   | 2    | Tuesday | 456   | 4    | Dead   | Girl
3   | Wednesday | Jan   | NULL | NULL    | NULL  | NULL | NULL   | NULL   
4   | Thursday  | Pete  | NULL | NULL    | NULL  | NULL | NULL   | NULL   

tblEDS、tblPatient 和 tblRecords 还有很多列,但此处未显示

So I'l try and explain the DB setup

tblEDS

eID | eVal1     | eVal2
-------------------------------------
1   | Monday    | Joe
2   | Tuesday   | Bob
3   | Wednesday | Jan
4   | Thursday  | Pete

tblPatient (I realise this isn't the best DB design around!)

pID | pVal1     | pVal2 | pEDS
------------------------------
1   | Monday    | 123   | 1
2   | Tuesday   | 456   | 2

tblPatientRecords

pID | rID
------------
1   | 1
1   | 2
2   | 3
2   | 4

tblRecords

rID | rVal1 | rVal2
-------------------
1   | Ok    | Boy
2   | Well  | Man
3   | Dead  | Kid
4   | Dead  | Girl

What I need is a query to pull back all records in tblAlert regardless of whether it has a corresponding value in tblPatient or not. For each record that exists in tblPatient, I need the relevant maximum rID and relevant values given back i.e.

eID | eVal1     | eVal2 | pID  | pVal1   | pVal2 | rID  | rVal1  | rVal2     
-------------------------------------------------------------------------
1   | Monday    | Joe   | 1    | Monday  | 123   | 2    | Well   | Man
2   | Tuesday   | Bob   | 2    | Tuesday | 456   | 4    | Dead   | Girl
3   | Wednesday | Jan   | NULL | NULL    | NULL  | NULL | NULL   | NULL   
4   | Thursday  | Pete  | NULL | NULL    | NULL  | NULL | NULL   | NULL   

There are a lot more columns for tblEDS, tblPatient and tblRecords but not shown here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

当爱已成负担 2024-10-24 05:06:07
SELECT
  a.eID,
  a.eValue,
  t.pID,
  t.pName,
  t.rID,
  t.rValue
FROM tblAlert a
  LEFT JOIN (
    SELECT
      p.pID,
      p.pName,
      p.eID,
      r.rID,
      r.rValue
    FROM tblPatient p
      INNER JOIN (
        SELECT pID, MAX(rID) AS rID
        FROM tblPatientRecords
        GROUP BY pID
      ) pr ON p.pID = pr.pID
      INNER JOIN tblRecords r ON pr.rID = r.rID
  ) t ON a.eID = t.eID
SELECT
  a.eID,
  a.eValue,
  t.pID,
  t.pName,
  t.rID,
  t.rValue
FROM tblAlert a
  LEFT JOIN (
    SELECT
      p.pID,
      p.pName,
      p.eID,
      r.rID,
      r.rValue
    FROM tblPatient p
      INNER JOIN (
        SELECT pID, MAX(rID) AS rID
        FROM tblPatientRecords
        GROUP BY pID
      ) pr ON p.pID = pr.pID
      INNER JOIN tblRecords r ON pr.rID = r.rID
  ) t ON a.eID = t.eID
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文