Mysql 使用“选择”在重复更新时插入

Mysql 使用“选择”在重复更新时插入,mysql,Mysql,我有存储用户信息的tableA,假设表中已经保存了一个用户(UserA),我想创建第二个(UserB),复制UserA拥有的数据 我会这样做: Insert into tableA (userID, switch, plateType, groupVal, brokeage) Select 'UserB', switch, plateType, groupVal, brokeage from tableA where userID = 'UserA'; 没关系 现在,有时我必须读

我有存储用户信息的
tableA
,假设表中已经保存了一个用户(
UserA
),我想创建第二个(
UserB
),复制
UserA
拥有的数据

我会这样做:

Insert into tableA (userID, switch, plateType, groupVal, brokeage)
  Select 'UserB', switch, plateType, groupVal, brokeage
  from tableA 
  where userID = 'UserA';
没关系

现在,有时我必须读取数据
UserA
has,并用它更新
UserB
,但需要在上面的相同查询中执行,因此我需要使用on replicate key

我写了这篇文章,但它不起作用,我一直在研究,但找不到我需要的答案:

Insert into tableA (userID, switch, plateType, groupVal, brokeage)
  Select 'UserB', switch, plateType, groupVal, brokeage
  from tableA
  where userID = 'UserA'
on duplicate key
  Update tableA brokeage = (Select brokeage
                            from tableA
                            where userID = 'UserA')
  where userID = 'UserB';
如果有人能看出这有什么问题,我将非常感谢您的帮助。

使用此:

Insert into tableA (userID, switch, plateType, groupVal, brokeage) 
Select 'UserB', switch, plateType, groupVal, brokeage 
from tableA 
where userID = 'UserA' 
on duplicate key Update brokeage = (
  Select brokeage 
  from tableA 
  where userID = 'UserA');

查看更新结构,它将更新重复行,无需精确表名的位置。

非常感谢Rob,它确实有效,但不是我预期的方式,因为在重复上无效…我的错误,我忘了写,主键使用自动递增值,我不知道这是否会影响,如果有,请告诉我