我有以下查询,效果很好,并显示 tbl_staff.staff_id 和 tbl_lead.rlog_create_user_id 值不匹配的位置。
SELECT
tbl_staff.staff_id,
tbl_staff.username,
tbl_lead.rlog_create_user_name,
tbl_lead.rlog_create_user_id
FROM
tbl_staff
JOIN tbl_lead ON tbl_staff.username = tbl_lead.rlog_create_user_name
AND tbl_staff.staff_id <> tbl_lead.rlog_create_user_id;
查询返回如下值,您可以看到 1014 与 1004 不匹配。
1014 bubba bubba 1004
我想将 tbl_lead.rlog_create_user_id 中的值更新为与 tbl_staff.staff_id 中找到的值相同。
我尝试插入 SET 命令,但它给了我一个一般语法错误:
SELECT
tbl_staff.staff_id,
tbl_staff.username,
tbl_lead.rlog_create_user_name,
tbl_lead.rlog_create_user_id
FROM
tbl_staff
JOIN tbl_lead ON tbl_staff.username = tbl_lead.rlog_create_user_name
AND tbl_staff.staff_id <> tbl_lead.rlog_create_user_id
SET tbl_lead.rlog_create_user_id=tbl_staff.staff_id ;
实际错误是:
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET tbl_lead.rlog_create_user_id=tbl_staff.staff_id' at line 10
我尝试使用此问题将 SELECT 更改为和 UPDATE 命令,但仍然无法使其工作:How can I do an UPDATE statement with JOIN in SQL?
最佳答案
试试这个
UPDATE tbl_lead
JOIN tbl_staff ON tbl_staff.username = tbl_lead.rlog_create_user_name
SET tbl_lead.rlog_create_user_id = tbl_staff.staff_id
WHERE tbl_staff.staff_id <> tbl_lead.rlog_create_user_id;
关于mysql - 使用 JOIN 命令更新记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51730325/