是否可以使用 Zend_Db_Table 一次更新多行? [英] Is it possible to update multiple rows at the time using Zend_Db_Table?

查看:22
本文介绍了是否可以使用 Zend_Db_Table 一次更新多行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的数组.

$a = array(
   array('id' => 2, 'year' => 2010),
   array('id' => 3, 'year' => 2011)
);

我有一个带有 id 主键的 MySQL 表.现在我像这样更新我的表:

And i have a MySQL table with id primary key. Now I update my table like this:

foreach ($a as $v) {
  $db->update($v, array('id = ?' => $v['id']));
}

而我的 $a 包含 3700 行.我需要创建一个大查询而不是循环.怎么办?先感谢您.对不起我的英语.

And my $a contains 3700 rows. I need to create one big query instead of loop. How can do it? Thank you in advance. Sorry for my english.

推荐答案

你的要求不常做,因为你通常会使用 update() 来设置大量记录相同的值或设置一个记录具有多个不同的值.

What your asking isn't often done, as you would usually use update() to either set a lot of records to have the same values or set one record to have many differnt values.

解决此问题的一种方法是汇总更新,因此使用您的数组获取年份为 2011 的所有 ID,然后运行:

One way around this is the aggregate the updates, so using your array get all the ids where the year is 2011 then run this:

 $where = array();

 // This where should contain all the ids that need the year set to 2011
 // E.g.
 $where[] = array("id" => 3);

 $db->update("table_name", array("year" => 2011), $where);

假设您在同一年有很多行,这样做会减少查询的数量.相关文档在此处.

Doing this will reduce the number of queries assuming you have many rows with the same year. The documentation for this is here.

或者你可以使用一种方法 像这样

Or you could use a method like this

在 OP 响应后编辑

问题的本质意味着它无法有效解决.

您要求一种使用非常不同的数据集更新 3,700 行数据的方法.如果数据集不同,则没有可以利用的模式来提高效率.查找模式,如同一年的行,并利用它们来提高查询速度,但需要一些大脑参与,如 regilero 指出的 array mashing 形式.

Your asking for a way of updating 3,700 rows of data with very different sets of data. If the sets of data are different then there is no pattern that you can exploit in order to make this effeicent. Finding patterns, like rows with the same year, and using them to your advantage will increase the speed of the query but will require some brain enagement in the form of array mashing as noted by regilero.

这篇关于是否可以使用 Zend_Db_Table 一次更新多行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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