且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MySQL-如何使用另一个表中的值更新表?

更新时间:2022-12-09 14:48:08

我们需要有关无法正常工作的详细信息,但我认为您只需要使用:

We need details about what's not working, but I think you only need to use:

UPDATE TablePeople 
   SET missingdate = (SELECT MAX(te.replacementDate)
                        FROM TABLEEVENT te
                       WHERE te.people_id = TablePeople.id)   
 WHERE missingdate IS NULL

注释

  • 由于担心您会从子查询中获取多个值,因此使用MAX来返回最新的替换日期
  • 如果TABLEEVENT中没有支持记录,则它将返回null,因此没有变化
  • Notes

    • MAX is being used to return the latest replacementdate, out of fear of risk that you're getting multiple values from the subquery
    • If there's no supporting record in TABLEEVENT, it will return null so there's no change