我有两张桌子:
work (work_id (AI, PK), sent_date, received_date, visit_date)
history_work(id_history_work (AI, PK), work_id (FK), sent_date, reseived_date, visit_date)
关系应该是1-> n。
我想更新工作表,所以sent_date,received_date和visit_date应该在history_work表(last id_history值)中具有最后插入记录的值,并且具有相同的work_id值。
答案 0 :(得分:1)
您可以使用join
执行此操作。加入history
表一次。再次加入以获得最大ID(可能是最近的插入)。
update work w join
history h
on w.work_id = h.work_id join
(select work_id, max(id_history_work) as maxihw
from history
group by work_id
) hw
on hw.maxihw = h.id_work_history
set w.sent_date = h.sent_date,
w.received_date = h.received_date,
w.visit_date = h.visit_date;