class test {
/**
* 创建像这样的查询: "IN('a','b')";
*
* @author wengxianhu
* @created to 2013-05-27
* @param mix $item_list 列表数组或字符串,如果为字符串时,字符串只接受数字串
* @param string $field_name 字段名称
* @return void
*/
public function db_create_in($item_list, $field_name = '')
{
if (empty($item_list))
{
return " ".$field_name . " IN ('') ";
}
else
{
if (!is_array($item_list))
{
$item_list = explode(',', $item_list);
foreach ($item_list as $k=>$v)
{
$item_list[$k] = intval($v);
}
}
$item_list = array_unique($item_list);
$item_list_tmp = '';
foreach ($item_list AS $item)
{
if ($item !== '')
{
$item_list_tmp .= $item_list_tmp ? ",'$item'" : "'$item'";
}
}
if (empty($item_list_tmp))
{
return " ".$field_name . " IN ('') ";
}
else
{
return " ".$field_name . ' IN (' . $item_list_tmp . ') ';
}
}
}
/**
* 拼接更新字段
*
* @author wengxianhu
* @created to 2013-05-27
* @param unknown $data 批量更新的数组
* @param string $index_key 主键值的字段名
* @return string
*/
public function _concatFields($data, $index_key)
{
if (empty($data)) {
return '';
}
$array_tmp = array();
$index_key_array = array();
if (!is_array(current($data))) {
$data = array($data);
}
$tmp1 = "";
foreach (current($data) as $_v => $_k) {
$tmp1 = $_v . '_temp';
${$tmp1} = "";
if ($_v != $index_key) {
${$tmp1} .= " {$_v} = CASE {$index_key} ";
}
}
reset($data);
$tmp2 = "";
foreach ($data as $_k => $_v) {
foreach ($_v as $_f => $_fv) {
$tmp2 = $_f . '_temp';
${$tmp2} .= "WHEN '{$_v[$index_key]}' THEN '" . addslashes(stripslashes($_fv)) . "' ";
array_push($index_key_array, $_v[$index_key]);
}
}
reset($data);
$tmp3 = "";
foreach (current($data) as $_v => $_k) {
$tmp3 = $_v . '_temp';
${$tmp3} .= " END ";
if ($_v != $index_key) {
$array_tmp[$tmp3] = ${$tmp3};
}
}
$array_tmp[$index_key] = $this->db_create_in($index_key_array, $index_key);
return $array_tmp;
}
/**
* 拼接更新字段
*
* @author wengxianhu
* @created to 2013-05-27
* @param unknown $data 批量更新的数组
* @param string $index_key 主键值的字段名
* @return string
*/
public function _concatFields_old($data, $index_key)
{
if(empty($data))
{
return '';
}
$array_tmp = array();
$index_key_array = array();
if(!is_array(current($data)))
{
$data = array($data);
}
foreach (current($data) as $_v => $_k)
{
if($_v != $index_key)
{
${$_v.'_temp'} .= " {$_v} = CASE {$index_key} ";
}
}
reset($data);
foreach ($data as $_k => $_v)
{
foreach ($_v as $_f => $_fv)
{
${$_f.'_temp'} .= "WHEN '{$_v[$index_key]}' THEN '".addslashes(stripslashes($_fv))."' ";
array_push($index_key_array, $_v[$index_key]);
}
}
reset($data);
foreach (current($data) as $_v => $_k)
{
if($_v != $index_key)
{
${$_v.'_temp'} .= " END ";
$array_tmp[$_v.'_temp'] = ${$_v.'_temp'};
}
}
$array_tmp[$index_key] = $this->db_create_in($index_key_array, $index_key);
return $array_tmp;
}
/**
* 获取更新的数据SQL
*
* @author wengxianhu
* @created to 2013-05-27
* @param unknown $data 批量更新的数组
* @param string $index_key 主键值的字段名
* @return multitype:
*/
public function _getUpdateInfo($data, $index_key)
{
reset($data);
$fields = array();
$conditions = array();
$fields_info = $this->_concatFields($data, $index_key);
$conditions = $fields_info[$index_key];
unset($fields_info[$index_key]);
$fields = implode(',', $fields_info);
return compact('fields', 'conditions');
}
/**
* 批量更新数据
*
* @author wengxianhu
* @created to 2013-05-27
* @param unknown $set 批量更新的数组
* @param string $table 数据表名
* @param string $index_key 主键值的字段名
* @return void
*/
public function updateAll($set, $table, $index_key)
{
if(empty($set))
{
return '';
}
$update_info = $this->_getUpdateInfo($set, $index_key);
$sql = "UPDATE {$table} SET {$update_info['fields']} WHERE {$update_info['conditions']}";
$this->query($sql,'execute'); //这里根据当前使用的系统修改
}
}
$goods = array(
array('goods_id'=>2000,'cate_id'=>100,'name'=>'godos1'),
array('goods_id'=>2001,'cate_id'=>101,'name'=>'godos2'),
array('goods_id'=>2002,'cate_id'=>102,'name'=>'godos3'),
);
// $goods = array('goods_id'=>2000,'cate_id'=>100,'name'=>'godos1');
$a = new test();
$a->updateAll($goods, 'ecm_goods', 'goods_id');
女装批发
分享到:
相关推荐
在PHP编程中,批量插入多条记录到数据库是常见的操作,尤其在处理大量数据时,可以显著提高效率。本文将详细讲解如何实现这一功能,并提供一个示例代码`insertAll.php`来帮助理解。 首先,我们需要了解基本的SQL...
这些数据通常存储在文件或者数据库表中,例如“update_to_1.6.8.txt”、“rollback.txt”和“update_to_1.6.9.txt”可能包含了这些更新信息。文件中的内容可能是SQL更新语句,或者是待处理数据的ID列表。 批量更新...
首先,批量添加数据是指一次向数据库中插入多条记录,而批量修改则是在已有的数据基础上进行更新。在tp5框架中,`saveAll()`方法是这两个操作的核心工具。这个方法允许我们传入一个包含多个数据对象(或数组)的集合...
这个方法接收一个包含多条记录的数组,并一次性插入数据库。示例如下: ```php $result = $userModel->insertAll($data); ``` 这里的`$result`会返回受影响的行数,如果插入成功,它应该是数据的长度。 4. **...
批量添加数据通常指的是将多条数据一次性插入到数据库中,这样可以有效减少数据库的交互次数,提高数据插入效率。在PHP中,可以使用SQL的INSERT INTO语句来完成这一操作。为了实现批量插入,可以在表单中设计相同的...
然而,Laravel的标准库并没有提供直接的批量更新功能,特别是针对多条记录的情况。本文将详细介绍如何在Laravel中实现批量更新多条数据。 首先,我们需要了解MySQL中的`CASE...WHEN`语句。这是一种非常实用的SQL...
批量更新操作通常用于一次性更新数据库中多条记录,例如,用户状态批量更改、商品价格批量调整等场景。传统的更新方法是使用foreach循环对数组中的每一个数据项逐一进行更新,但这种方法效率低下,当数据量庞大时还...
在MySQL中,批量更新多条记录的同一个字段为不同的值是一项常见的操作,特别是在处理大量数据时,为了提高效率,我们需要避免循环执行单独的UPDATE语句。以下是一些关于如何高效地进行批量更新的方法。 首先,最...
1. 新闻管理系统:在新闻管理系统中,可以使用 PHP 实现批量删除数据来删除多个新闻。 2. 电子商务系统:在电子商务系统中,可以使用 PHP 实现批量删除数据来删除多个商品。 3. 博客系统:在博客系统中,可以使用 ...
本文将深入探讨两种常见的批量更新方法:使用`UPDATE`语句和`REPLACE INTO`语句,并介绍如何在特定情况下使用`CASE`表达式实现批量更新多条记录的不同值。 首先,基本的`UPDATE`语句用于更新单个或多个记录的相同...
在执行批量更新时,首先需要判断是否需要更新某条记录。如果记录已存在,则执行更新操作;如果记录不存在,则需要执行插入操作。这个过程可以通过条件判断来完成,并且通常会用到循环结构来处理多个数据项。 在提供...
在MySQL中,一次更新多条记录通常涉及使用`UPDATE`语句配合条件子句来实现。当需要批量更新数据时,尤其是当更新的条件是基于特定的值或规则时,可以使用`CASE`表达式来指定不同情况下的更新值。在给出的示例中,...
但在更新版本中,新增了`insert_batch()`方法,它允许开发者通过一次数据库操作来插入多条记录,极大地提高了效率和性能。 在使用`insert_batch()`方法前,需要先准备要插入的数据。数据应该以数组的形式组织,其中...
例如,可以一次性插入多条记录: ```sql INSERT INTO user_info (name, age) VALUES ('name1', 18), ('name2', 19); ``` 在PHP中实现这一方法如下: ```php // 假设$arr 是包含大量数据的数组 $sql = "INSERT ...
在SQL语言中,更新多条记录的字段值且每行记录的字段值各不相同时,通常有两种常用的方法。一种是使用CASE语句结合IF-THEN逻辑,另一种是利用INSERT语句的DUPLICATE KEY UPDATE特性。这两种方法都可以避免循环执行多...