Drupal 7 |使用db_update更新多个行 [英] Drupal 7 | update multiple rows using db_update

查看:122
本文介绍了Drupal 7 |使用db_update更新多个行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,如

  Array(
[1] => 85590762,22412382,97998072
[3] => 22412382



其中key是item_id,value是我需要更新的列的值。我可以在循环中使用db_update,但是由于性能,我想避免这种策略。我想更新单个数据库调用中的所有行。另外使用db_query我认为不会是个好主意。那么有没有办法使用db_update来更新这些行?



根据上面的数据,标准的mysql查询将像

 更新项目已售出= 1,users ='85590762,22412382,97998072'其中item_id = 1; 
更新项集已售= 1,users ='22412382'其中item_id = 3;


解决方案

我想你可以做一些像

  $ query = db_udpate('table') - > fields(array('field_1','field_2','field_3')); 
foreach($ fields as $ record){
$ query-> values($ record);
}
return $ query-> execute();

其中$ fields = array('field_1'=> VALUE,'field_2'=> VALUE) / p>

I have an array like

Array (
    [1] => 85590762,22412382,97998072
    [3] => 22412382 

)

Where key is the item_id and value is the value of a column which I need to update against an item. I can use db_update in a loop but i want to avoid this strategy due to performance. I want to update all the rows in a single db call. Also using db_query I think will not be a good idea. So is there any way using db_update to update these rows?

According to above data, standard mysql queries will be like

update items set sold= 1, users = '85590762,22412382,97998072' Where item_id = 1; 
update items set sold = 1, users = '22412382' Where item_id = 3;

解决方案

I think you could do something like

$query = db_udpate('table')->fields(array('field_1','field_2','field_3'));
  foreach ($fields as $record) {
    $query->values($record);
  }
return $query->execute();

Where $fields = array ('field_1' => VALUE, 'field_2' => VALUE)

这篇关于Drupal 7 |使用db_update更新多个行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆