简体   繁体   English

具有相同ID的MySQL更新字段

[英]MySQL update field with same ID

I have a table that has two different values for the same product id: 我有一个表,该表具有相同产品ID的两个不同值:

FROM _term_relationships 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. 如果第二个值是179,我想将第一个值(2)更改为4。

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: 我目前正在使用此查询来将所有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

But now I need a dependency for category value that is 197. 但是现在我需要对类别值为197的依赖项。

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 查找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

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

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