MySQL 更新具有相同 ID 的字段

MySQL update field with same ID

我有一个 table 对同一产品 ID 有两个不同的值:

来自 _term_relationships

object_id   term_taxonomy_id 

3148        2   
3148        179     
3149        2   
3149        180     

如果第二个值为 179,我想将第一个值 (2) 更改为 4。

我该怎么做?

我目前正在使用此查询将所有 term_taxonomy_id 字段的值从 2 更改为 4:

UPDATE
    infy_term_relationships 
    LEFT JOIN infy_posts ON ( infy_term_relationships.object_id = infy_posts.ID )
SET infy_term_relationships.term_taxonomy_id =4
WHERE
    infy_posts.post_type = 'product'
    AND infy_term_relationships.term_taxonomy_id =2

但现在我需要一个类别值 197 的依赖项。

查找 object_id int 以记录 2 和 179 并且 object_id 和 term_taxonomy_id 等于 2 将 term_taxonomy_id 设置为 4

drop table if exists t1;
create table t1 (object_id int, term_taxonomy_id int);
insert into t1 values 
(3148,     2),
(3148,      179),  
(3149,       2), 
(3149,        180);

update t1 
  set term_taxonomy_id = 4 
  where term_taxonomy_id = 2 
    and object_id = 
        (select object_id 
           from (select object_id  
                   from t1 
                   group by object_id 
                   having concat(',',group_concat(term_taxonomy_id),',')  like  '%,2,179,%' 
                 ) as temp    
          );  

select * from t1