MySQL服务器 - 更新键值对中的值的值

时间:2016-11-04 08:45:45

标签: mysql

enter image description here

我想使用密钥Training Classess将数据Training更新为features_title_1,并希望在单个查询中对其余记录执行相同操作。我将如何执行此操作?

我知道我的查询是错误的,但在这里......任何帮助都将受到赞赏..

"update home_content set 
`features_title_1`    ='Training Classes' , 
`features_title_2`    ='Happy Clients' ,
`features_title_3`    ='Trainings Rooms' ,
`gender_title_women`  ='For Womendsd',
`gender_desc_women`   ='Nulla sed lacus vitae risus, 
`gender_title_men`    ='For Men',
`gender_desc_men`     ='Nulla sed lacus vitae."

2 个答案:

答案 0 :(得分:1)

您可以在执行mysql查询之前简单地创建一个数组 像这样

$DataAarray =  array("features_title_1"    =>'Training Classes' , 
                            "features_title_2"    =>'Happy Clients' ,
                            "features_title_3"    =>'Trainings Rooms' ,
                            "gender_title_women"  =>'For Womendsd',
                            "gender_desc_women"   =>'Nulla sed lacus vitae risus', 
                            "gender_title_men"    =>'For Men',
                            "gender_desc_men"     =>'Nulla sed lacus vitae');

然后你就可以拥有这样的foreach并在foreach内部执行你的mysql更新查询

foreach($DataAarray as $Key => $value){            
    //Updte query here 
    UPDATE `table` SET `value` = $value WHERE `mkey` = $Key;
}

答案 1 :(得分:0)

您可以使用case子句在单个查询中执行此操作,而不是运行许多更新查询。您的查询看起来像这样:

更新home_content SET值=(时(mkey ='features_title_1')THEN'培训课程'时(mkey ='features_title_2')THEN'快乐客户'END)mkey IN('features_title_1','features_title_2') ;