在医院的数据库里工作。我有一张病人表,另一张表包含医疗记录。医疗记录表,有patient_id。由于系统中的错误,许多患者被插入了两次或更多,具有相同的id。我正在尝试执行一个mysql查询,该查询允许我删除/检查患者表中重复的患者,但前提是patient_id不在医疗记录表中。
如下所示:
(select * from patients group by id having count(id)>1 as p)
where patient_id not in (select patient_id from history)上面的查询是象征性的,它不起作用。
发布于 2014-08-08 21:23:31
选择所有不在history中的重复patients的一种方法是使用where not exists
select p.id
from patients p
where not exists (
select h.patient_id
from history h
where h.patient_id=p.id
)
group by p.id having count(p.id) > 1另一种方法是使用not in
select id
from patients
where id not in (
select patient_id from Posts
WHERE patient_id IS NOT NULL
group by patient_id
)
group by id having count(id) > 1https://stackoverflow.com/questions/25204248
复制相似问题