我的MySql服务器中有2个不同的数据库.
First table DB1.contacts: id | name | code 1 | foo | 157 2 | foo | 95 3 | foo | 210 Second table DB2.paperworks: id | name | contact_id 1 | foo | 0
我想更新DB2.paperworks,设置DB1.contacts.contacts表的DB2.paperworks.contact_id = max(DB1.contacts.code),其中DB2.paperworks.name = DB1.contacts.name
我希望的输出应该是:
查询DB2.paperworks之后的第二个表:
id | name | contact_id 1 | foo | 210
这是我的查询:
UPDATE DB2.paperworks JOIN DB1.contacts ON DB2.paperworks.name = DB1.contacts.name SET DB2.paperworks.contact_id = DB1.contacts.code
我不明白如何写che"MAX(代码)"条件.你能帮我吗?
稍微简单一点的形式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 );