复杂又高效的SQL

2022/3/29 19:26:41

本文主要是介绍复杂又高效的SQL,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

update hd_mach_usage_rec d
join (
select us.id,us.turn_off_time,us.hd_mach_id,us.fk_bed_area_id,us.fk_bed_id
,md.in_end_time,md.fk_dr_id,dr.end_time
from hd_mach_usage_rec us inner JOIN
(SELECT fk_dr_id,hd_mach_id,fk_bed_area_id,in_end_time,fk_bed_id
FROM hd_mach_disinfect_rec WHERE in_end_time IS NOT NULL
GROUP BY hd_mach_id,fk_bed_id,fk_bed_area_id,in_end_time) md ON md.hd_mach_id = us.hd_mach_id
and us.fk_bed_area_id = md.fk_bed_area_id and us.fk_bed_id=md.fk_bed_id and us.turn_off_time = md.in_end_time
LEFT JOIN (select id,end_time from hd_dr) dr on md.fk_dr_id = dr.id
WHERE us.fk_hosp_id = 20017001
) t on d.id = t.id
set d.turn_off_time = t.end_time;

 

-- leftjoin 比在select写子查询快的多

-- 床位 区域 机器 在一天的不同时间是可以多条记录的 所以还需要结束时间作区分

-- 此sql为批量更新 , 链接了3张表, usage_rec 的turn_off_time需要更为dr的endtime,然而drid需要去disinfect表拿, usage和disinfect关联需要4个字段(床位id,区域id,机器id,结束时间),因为一开turnofftime用的就是disinfect的inendtime



这篇关于复杂又高效的SQL的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程