php怎么更新多条数据,PHP中批量更新数据表中多条记录
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怎么更新多条数据,PHP中批量更新数据表中多条记录相关推荐
- Mysql 一条SQL语句实现批量更新数据,update结合case、when和then的使用案例
如何用一条sql语句实现批量更新?mysql并没有提供直接的方法来实现批量更新,但是可以用点小技巧来实现. 代码如下: UPDATE mytable SET myfield = CASE id WHE ...
- mysql 批量更新数据 备份_mysql 批量更新与批量更新多条记录的不同值实现方法...
批量更新 mysql更新语句很简单,更新一条数据的某个字段,一般这样写: UPDATE mytable SET myfield = 'value' WHERE other_field = 'other ...
- sql进行批量更新或者一条sql写出批量更新的语句
有表如下 t_user:字段如下 id,name,phone t_phone:字段如下 uid,phone 其中t_phone表的uid字段是外键并且唯一,他引用t_user的id字段,现需要将t_p ...
- mybatis中批量更新
一.更新多条数据,每条数据都不一样 背景描述:通常如果需要一次更新多条数据有两个方式,(1)在业务代码中循环遍历逐条更新.(2)一次性更新所有数据(更准确的说是一条sql语句来更新所有数据,逐条更新的 ...
- SQL Server数据库中批量导入数据的四种方法总结
在软件项目实施的时候,数据导入一直是项目人员比较头疼的问题.其实,在SQL Server中集成了很多成批导入数据的方法.有些项目实施顾问头疼的问题,在我们数据库管理员眼中,是小菜一碟.现在的重点就是, ...
- mysql从一个表查出写入另一个表_sql语句 怎么从一张表中查询数据插入到另一张表中...
展开全部 sql语句从一张表中查询数据插入到另一张表中的方法如下:e68a843231313335323631343130323136353331333365653762 1.select * int ...
- SQL Server数据库中批量导入数据
在软件项目实施的时候,数据导入一直是项目人员比较头疼的问题.其实,在SQL Server中集成了很多成批导入数据的方法.有些项目实施顾问头疼的问题,在我们数据库管理员眼中,是小菜一碟.现在的重点就是, ...
- mybatis mysql 批量更新_mysql批量update更新,mybatis中批量更新操作
在日常开发中,有时候会遇到批量更新操作,这时候最普通的写法就是循环遍历,然后一条一条地进行update操作.但是不管是在服务端进行遍历,还是在sql代码中进行遍历,都很耗费资源,而且性能比较差,容易造 ...
- mysql插入多行数据来自另一张表_mysql从一张表查询批量数据并插入到另一表中的完整实例...
说在前面 nodejs 读取数据库是一个异步操作,所以在数据库还未读取到数据之前,就会继续往下执行代码. 最近写东西时,需要对数据库进行批量数据的查询后,insert到另一表中. 说到批量操作,让人最 ...
- hive插入多条数据sql_Hive/Impala批量插入数据
问题描述 现有几千条数据,需要插入到对应的Hive/Impala表中.安排给了一个同事做,但是等了好久,反馈还没有插入完成--看到他的做法是:对每条数据进行处理转换为对应的insert语句,但是,实际 ...
最新文章
- jQuery中$(function(){})与(function($){})(jQuery)的区别
- C++ main函数中参数argc和argv
- SAP QM初阶之检验批系统状态中的CTCM该如何消除?
- 检查一列数据的重复项 vba_01.VBA自动化办公——数据的局部加和
- C++又一坑:动态链接库中的全局变量
- too many connections 解决方法
- [记录]---mysql数据库,decimal类型设置默认值0,不要是null
- 大白话5分钟带你走进人工智能-第二十四节决策树系列之分裂流程和Gini系数评估(3)...
- angularJs模版注入的两种方式
- 佳能lbp611cn硒鼓_佳能lbp611cn说明书
- unity之EasyAR使用
- php revel,golang,revel_Revel 的路由问题,golang,revel - phpStudy
- arduino 嗡鸣器 音乐_arduino笔记一:用arduino实现蜂鸣器播放音乐
- Python技术pip的使用命令总结
- 什么是NP-Hard
- mac文件反选_【PS反选键是什么?】Photoshop该如何进行反向选择?
- 2022-2028全球与中国液态环氧树脂市场现状及未来发展趋势
- php删除bom,php bom如何去掉
- 手机摄影最常用的 5 种构图方式
- discord怎么创建账号_如何创建Ultimate Discord服务器
热门文章
- [html] 举例说明当我们在写布局时,都有哪些边界的情况需要关注的?
- 前端学习(1341):mongoose验证规则延伸
- 前端学习(159):meta
- 歌谣带你看java面试题
- Java:这是一份全面 详细的 Synchronized关键字 学习指南
- linux mysql主从配置
- 10个实用的机器学习建议
- mysql 静态表 是不是 myisam_mysql的静态表和动态表的区别,MyISAM和InnoDB的区别
- python网格划分_在python中创建一个2d网格
- MySQL系列(一) MySQL体系结构概述