Csaba Molnár
Csaba Molnár

Reputation: 53

MySQL update field with same ID

I have a table that has two different values for the same product id:

FROM _term_relationships

object_id   term_taxonomy_id 

3148        2   
3148        179     
3149        2   
3149        180     

I want to change the first value (2) to 4 if the second value is 179.

How can I do this?

I'm currently using this query to change all of term_taxonomy_id fields with values of 2 to 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

But now I need a dependency for category value that is 197.

Upvotes: 0

Views: 587

Answers (1)

splash58
splash58

Reputation: 26153

Find object_id int with to record 2 and 179 and for that object_id and term_taxonomy_id is equal 2 set term_taxonomy_id to 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

Upvotes: 1

Related Questions