简体   繁体   English

SQL查询:3个表的最高值加上行的其余部分再加上

[英]SQL Query: 3 tables highest Value plus rest of row plus

i'm currently stuck on a sql-query, trying to find a solution, but making me headache for 2 stays now. 我目前停留在sql查询上,试图找到解决方案,但现在让我头痛2。 i've got 3 tables 我有3张桌子

user-table: 用户表:

+-----+----------+-----------+
| pid | username |   role    |
+-----+----------+-----------+
|   1 | user1    | patient   |
|   2 | user2    | patient   |
|   3 | user3    | doc       |
|   4 | user4    | assistant |
|   5 | user5    | patient   |
+-----+----------+-----------+

base-dat: 设在:

+-----+---------+-------+------------+
| pid | surname | name  | birthdate  |
+-----+---------+-------+------------+
|   1 | smith   | john  | 1950-07-31 |
|   2 | jackson | sarah | 1948-08-15 |
+-----+---------+-------+------------+

med-dat: MED-DAT:

+-----+-----+---------------+--------+--------+
| mid | pid | dateLastEntry | weight | pulse  |
+-----+-----+---------------+--------+--------+
|   1 |   1 | 2017-12-01    |     86 |     65 |
|   2 |   1 | 2017-12-02    |     84 |     70 |
|   3 |   1 | 2017-12-03    |     80 |     67 |
|   4 |   2 | 2017-11-15    |     66 |     60 |
|   5 |   2 | 2017-11-17    |     60 |     64 |
+-----+-----+---------------+--------+--------+

I'm trying to get the max(dateLastEntry) for each user with role patient, showing their pid, name, surname, weight, pulse in a single row - , even if there is no med-data entry for the patient: something like this: 我正在尝试为具有角色患者的每个用户获取max(dateLastEntry),在一行中显示其pid,名称,姓氏,体重,脉搏-,即使该患者没有医学数据输入:类似这个:

+-----+---------+-------+------------+--------+-------+
| pid | surname | name  | lastEntry  | weight | pulse |
+-----+---------+-------+------------+--------+-------+
|   1 | smith   | john  | 2017-12-02 | 84     | 70    |
|   2 | jackson | sarah | 2017-11-17 | 60     | 64    |
|   5 | NONE    | NONE  | NONE       | NONE   | NONE  |
+-----+---------+-------+------------+--------+-------+

Atm my statement looks like this, but can't get the proper result: Atm我的陈述看起来像这样,但无法获得正确的结果:

select b.pid, s.surname, s.name, max(m.date) as lastEntry, m.weight, m.pulse
from users b 
left join med-dat m on b.pid = m.pid
left join base-dat s on m.pid = s.pid
where b.role = 'Patient'
group by b.pid, s.surname, s.name, m.weight;

You can rewrite your query as below to get the desired output 您可以按以下方式重写查询以获取所需的输出

select b.pid, s.surname, s.name, m.dateLastEntry as lastEntry, m.weight, m.pulse
from users b 
left join med_dat m on b.pid = m.pid
left join base_dat s on m.pid = s.pid
left join med_dat m1 on m.pid = m1.pid 
                     and m.dateLastEntry < m1.dateLastEntry
where m1.pid is null
and b.role = 'Patient'

DEMO DEMO

Edit from comment, join base_dat using pid from user table 从注释中编辑,使用用户表中的pid加入base_dat

select b.pid, s.surname, s.name, m.dateLastEntry as lastEntry, m.weight, m.pulse
from users b 
left join med_dat m on b.pid = m.pid
left join base_dat s on b.pid = s.pid
left join med_dat m1 on m.pid = m1.pid and m.dateLastEntry < m1.dateLastEntry
where m1.pid is null
and b.role = 'Patient'
group by b.pid, s.surname, s.name, m.weight;

Demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM