【发布时间】:2014-01-27 18:26:16
【问题描述】:
我知道这个错误的原因,但我不知道如何解决它。情况如下:
我有一个 tableA,其中包含下一列 userID、switch、plateType、groupVal、brokeage。
userID、plateType 和 groupVal 代表唯一的密钥。 groupVal 和 userId 可能有许多不同的值,但 plateType 只能有 4(A,B,C,D)。
一个userId 和一个groupVal 的记录示例如下:
Userid-------plateType------ groupVal------- brokeage
UserA--------- A----- ------------ 3--------------- 5.5
UserA--------- B----- ------------ 3--------------- 7.6
UserA--------- C----- ------------ 3--------------- 2.1
UserA--------- D----- ------------ 3--------------- 3.5
因此,当我运行下一个查询时,如果此记录不存在,则将创建该记录,但如果它们存在,则应该对其进行更新,但是我得到的是这个错误:
基数违规:1242 子查询返回多于 1 行
Insert into tableA (userID, plateType, groupVal, brokeage)
Select 'UserB', plateType, groupVal, brokeage from tableA
where userID = 'UserA'
on duplicate key
Update brokeage = (Select brokeage from tableA where userID = 'UserA')
我知道它在子查询中,我在更新中使用的选择...所以我真正需要的是从tableA 中选择userId = UserA 但与groupVal 和@ 匹配的记录987654339@ 必须更新为 UserB。我希望我说清楚,有人可以给出答案。
【问题讨论】:
标签: mysql