使用场景,表id有序增长,如果可以修改代码,将上一条记录id插到当前记录中更容易处理
select DISTINCT newmeasure.outpatient_no,newmeasure.name,pre.current_gestational_week,
pem.systolic,pem.diastolic,pem.systolic_2,pem.diastolic_2,pem.systolic_3,pem.diastolic_3,
pem.temperature,pem.pulse,pem.height,pem.weight,ppr.preweight,ppr.bmi,
newmeasure.create_date,newmeasure.next_create_date,nextpem.height as next_height,newmeasure.next_mea_id,nextpem.weight as next_weight,
pre.lmp,pre.sure_edd,pre.edd
from (
select mea.*,max(meamin.create_date) as next_create_date,max(meamin.id) as next_mea_id,max(pemin.id) as next_prmin_id
from measure mea
LEFT JOIN measure meamin on (meamin.outpatient_no=mea.outpatient_no and meamin.create_date<mea.create_date)
LEFT JOIN physical_exam_measure pemin on (pemin.id=meamin.physical_exam_measure_id)
where mea.outpatient_no is not null and mea.outpatient_no !=''
GROUP BY mea.id
) newmeasure
LEFT JOIN pregnancy pre on newmeasure.outpatient_no=pre.outpatient_no
LEFT JOIN physical_exam_measure nextpem on nextpem.id=newmeasure.next_prmin_id
LEFT JOIN physical_exam_measure pem on newmeasure.physical_exam_measure_id=pem.id
LEFT JOIN personal_profile ppr on pre.personal_profile_id=ppr.id
WHERE 1=1
------20230411改进-----
可以改进代码的前提下,在当天检查的数据插入上一条的id,再加上索引