【发布时间】:2020-07-26 23:00:43
【问题描述】:
我有如下表结构
TABLE A
Productid price groupId
1 100 A
2 99 A
3 0 A
4 50 B
5 49 B
6 0 B
我用来自表 B 的价格填充表 A,加入 Id。有时表 B 没有价格。 如果 b 没有价格,我想将价格更新为同一组的另一个价格,因为我的价格不能为零。
有没有办法根据组使用自身更新表价格列?例如将 productId 3 price 更新为其组中另一个产品的价格(1 或 2)
TABLE A after update
Productid price groupId
1 100 A
2 99 A
3 100 A
4 50 B
5 49 B
6 49 B
这似乎很愚蠢,但这些是业务规则(这很有意义,我为示例简化了问题)
当我尝试以下操作时出现错误:
update 'Table A' t1
join (select price ,groupId from 'table A' where Price > 0 group by
groupId) as t2
on t1.groupId = t2.GroupId
SET t1.Price = t2.Price
(conn=58292) Can't reopen table: 'Table A'
我曾想过创建第三个临时表,但这似乎......错了?我确信必须有一种方法可以使用更新语句和连接来做到这一点
【问题讨论】:
-
tableA是临时表吗? -
不,表 A 是永久表
标签: mysql sql sql-update mariadb