具有相同ID

时间:2015-07-31 12:53:55

标签: mysql sql-update

我有一个表,对于相同的产品ID有两个不同的值:

FROM _term_relationships

object_id   term_taxonomy_id 

3148        2   
3148        179     
3149        2   
3149        180     

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

我该怎么做?

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

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的类别值依赖。

1 个答案:

答案 0 :(得分:0)

使用记录2和179查找object_id int,并且对于该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