时间:2021-07-01 10:21:17 帮助过:14人阅读
数据库的update 如果使用case when 的话,也是可以批量更新的,本人在百度上找到了一个基于tp的saveAll()方法,用于更新数据
本文主要讲关于批量insert;
例:
生成一个订单
正常情况的语句为:
INSERT INTO order (`goods_id`,`num`,`price`) VALUES (1,1,'10.00'); //封装成函数 function add_order($goods_id,$num,$price){ $db->query("INSERT INTO order (`goods_id`,`num`,`price`) VALUES ($goods_id,$num,$price)"); }
假设有一个用户,一次性将购物车的1000个商品结算成订单,生成1000个订单时;
for ($i=0;$i<1000;$i++){ $db->query("INSERT INTO order (`goods_id`,`num`,`price`) VALUES ($goods_id,$num,$price)"); } //这样的话会导致服务器资源占用过大,网站卡死 //所以,我们可以 $sql = "INSERT INTO order (`goods_id`,`num`,`price`) VALUES "; for ($i=0;$i<1000;$i++){ if($i==0){ $sql.="($goods_id,$num,$price)"; }else{ $sql.=",($goods_id,$num,$price)"; } } $db->query($sql);
大概意思就是这样了,批量更新实现比较麻烦一点,就不发了,以下是批量更新的sql执行语句
UPDATE tiyan.dm_user_cupboard SET `res_id` = CASE `id` WHEN 1041 THEN '1' WHEN 1058 THEN '1' WHEN 1055 THEN '1' END,`food_code` = CASE `id` WHEN 1041 THEN '68' WHEN 1058 THEN '47' WHEN 1055 THEN '49' END,`food_name` = CASE `id` WHEN 1041 THEN '红枣' WHEN 1058 THEN '莲藕' WHEN 1055 THEN '洋葱' END,`num` = CASE `id` WHEN 1041 THEN '2' WHEN 1058 THEN '3' WHEN 1055 THEN '2' END,`level` = CASE `id` WHEN 1041 THEN '2' WHEN 1058 THEN '2' WHEN 1055 THEN '2' END,`update_time` = CASE `id` WHEN 1041 THEN '2017-12-09 21:40:06' WHEN 1058 THEN '2017-12-09 21:40:06' WHEN 1055 THEN '2017-12-09 21:40:06' END WHERE id IN ( 1041,1058,1055 )
附带基于tp的saveAll()
//批量更新 public function saveAll($datas,$model){ ( $model || $model=$this->tableName); $model=empty($model)?$this->name:$model; $sql = ''; //Sql $lists = []; //记录集$lists $pk = $this->getPk();//获取主键 foreach ($datas as $data) { foreach ($data as $key=>$value) { if($pk===$key){ $ids[]=$value; }else{ $lists[$key].= sprintf("WHEN %u THEN '%s' ",$data[$pk],$value); } } } foreach ($lists as $key => $value) { $sql.= sprintf("`%s` = CASE `%s` %s END,",$key,$pk,$value); } $sql = sprintf('UPDATE __%s__ SET %s WHERE %s IN ( %s )',strtoupper($model),rtrim($sql,','),$pk,implode(',',$ids)); return M()->execute($sql); }
相关推荐:
php数据入库前清理 注意php intval与mysql的int取值范围不同_PHP教程
基于PHP的简单采集数据入库程序实例详解
基于PHP的简单采集数据入库程序,php采集入库_PHP教程
以上就是php一次性大量数据入库如何解决的详细内容,更多请关注Gxl网其它相关文章!