【发布时间】:2013-03-07 16:13:25
【问题描述】:
假设我有一个父子数据库,而子数据库保存着父子发生的事情的一种运行记录:
create table patient (
fullname text not null,
admission_number integer primary key
);
create table history (
note text not null,
doctor text not null,
admission_number integer references patient (admission_number)
);
(只是一个例子,我不是在做医疗应用)。
history 将有许多相同admission_number 的记录:
admission_number doctor note
------------------------------------
3456 Johnson Took blood pressure
7828 Johnson EKG 120, temp 99.2
3456 Nichols Drew blood
9001 Damien Discharged patient
7828 Damien Discharged patient with Rx
所以,我的问题是,我将如何构建一个查询来让我在注释字段中搜索和/或不搜索 patient 记录,例如,如果我想查找其历史记录包含的每位患者“血压”和“出院”。
现在我一直在 history 上进行 select 分组 admission_number,将所有注释与 group_concat(note) 结合起来并在 having 中进行搜索,因此:
select * from history
group by admission_number
having group_concat(note) like '%blood pressure%'
and group_concat(note) like '%discharged';
这行得通,但它使某些详细说明变得非常复杂 - 例如,我希望能够询问“每位患者的病史包含‘血压’并且其与 Damien 医生的病史为‘出院, "并且在我的基本查询之上建立这样的资格非常混乱。
有没有更好的方式来表达我的基本查询?
【问题讨论】: