Mysql: Update table with select max of another table

This question already has an answer here:

  • How do I UPDATE from a SELECT in SQL Server? 27 answers

  • 一个稍微简单的update形式可以做到这一点:

    UPDATE DB2.paperworks
    SET DB2.paperworks.contact_id = (
        select max(DB1.contacts.code)
        from DB1.contacts
        where DB1.contacts.name = DB2.paperworks.name
        group by DB1.contacts.code
    );
    

    尝试这个:

    UPDATE DB2.paperworks
    SET DB2.paperworks.contact_id = (
        SELECT MAX(DB1.contacts.code)
        FROM DB1.contacts
        WHERE DB2.paperworks.name = DB1.contacts.name
    )
    
    链接地址: http://www.djcxy.com/p/16866.html

    上一篇: 只在其不为空的情况下更新

    下一篇: Mysql:使用另一个表的select max更新表