简体   繁体   English

Mysql 根据不同的行更新表中的行值

[英]Mysql update row value in table based on different row

I have the following table structure我有以下表结构

TABLE A   
    Productid    price   groupId  
     1             100      A
     2             99       A
     3              0       A
     4             50       B
     5             49       B
     6              0       B

I populate table A with prices from table B joining on Id.我用来自表 B 的价格填充表 A,加入 Id。 Sometimes table B doesn't have prices.有时表 B 没有价格。 In cases were b doesn't have price I want to update the price to be another price from that same group, as I can't have a price of zero.如果 b 没有价格,我想将价格更新为同一组的另一个价格,因为我的价格不能为零。

Is there an way to update table a price column using itself based on group?有没有办法根据组使用自身更新表价格列? eg update productId 3 price to be the price of another product in it's group (1 or 2)例如,将 productId 3 的价格更新为该组中另一个产品的价格(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

It seems silly but these are the business rules (it makes sense irl I simplified the problem for the example)看起来很傻,但这些是业务规则(这很有意义,我简化了示例的问题)

When I tried the following I got Error:当我尝试以下操作时出现错误:

    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' 

I've thought of creating a third temporary table but that seems.... wrong?我曾想过创建第三个临时表,但这似乎......错了? I am sure there must be a way to do this using update statement and joins我确信必须有一种方法可以使用更新语句和连接来做到这一点

I would phrase the query as:我会将查询表述为:

update tablea a
inner join (select groupId, max(price) price from tablea group by groupId) a1 
    on a1.groupId = a.groupId
set a.price = a1.price
where a.price = 0 and a1.price > 0

Notes:笔记:

  • the table name should be surrounded with single quotes (those stand for literal strings) - if your table name really contains spaces, then use backticks for quoting (or better, yet, fix the table name!)表名应该用单引号括起来(那些代表文字字符串) - 如果您的表名确实包含空格,那么使用反引号进行引用(或者更好的是,修复表名!)

  • I changed the subquery to make it a valid aggregation query - yours has non-aggregated columns that do not belong to the group by clause, which is not a good practice, and might generate errors, depending on the SQL mode of your database我更改了子查询以使其成为有效的聚合查询-您的子查询具有不属于group by子句的非聚合列,这不是一个好习惯,并且可能会产生错误,具体取决于数据库的 SQL 模式

In this demo on DB Fiddlde with your sample data, the content of the table after update is:DB Fiddlde 上的这个演示中,使用您的示例数据,更新后的表格内容为:

Productid | price | groupId
--------: | ----: | :------
        1 |   100 | A      
        2 |    99 | A      
        3 |   100 | A      
        4 |    50 | B      
        5 |    49 | B      
        6 |    50 | B      

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM