跨多个表获取值(包括 MAX 和 NULL)
因此,我将尝试解释数据库设置
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)