使用另一个数据库更新一个数据库

时间:2014-03-12 17:04:33

标签: mysql

我有这个查询

SELECT pu.payroleId, u.userId, u.firstName, u.lastName
FROM users AS u, payrole_users AS pu
WHERE pu.firstName = u.firstName
AND pu.lastName = u.lastName
LIMIT 0 , 60

如果名称与userId表中的名称匹配(如果我的查询正确!),则会从users表中选择所有payrole_users。我需要的是:对于每个userId,我需要更新payrole_users.intranetId字段。这可能只与一个查询有关,还是我需要将所有这些存储为数组,然后在数组之后循环然后更新它?

2 个答案:

答案 0 :(得分:1)

尝试

UPDATE users u, payrole_users pu
SET pu.intranetId=u.userId
WHERE pu.firstName = u.firstName AND pu.lastName = u.lastName;

答案 1 :(得分:0)

尝试测试此查询 - 它能做你想做的吗?

UPDATE 
    payrole_users AS pu
        JOIN
    users AS u ON pu.firstName = u.firstname AND pu.lastName = u.lastName
SET
    pu.intranetId = u.userId

请注意:

FROM tableA AS A JOIN tableB AS B ON A.id = B.id

实际上与此相同:

FROM tableA AS A, tableB AS B WHERE A.id = B.id

有关详细信息,请参阅Explicit vs implicit SQL joins