Home  >  Article  >  Backend Development  >  批量处理mysql数据时,怎么在另一个表中插入所有记录?请高手能直接给出代码,多谢

批量处理mysql数据时,怎么在另一个表中插入所有记录?请高手能直接给出代码,多谢

WBOY
WBOYOriginal
2016-06-13 11:54:401017browse

批量处理mysql数据时,如何在另一个表中插入所有记录?请高手能直接给出代码,谢谢!
.比如数据库member表中有若干条数据,想将每一条数据中的二个字段cca和ccb相加得到的和插入到另一字段ccd中:

cca   ccb    ccd        id
45      66      0          1
67      36      0         2
86      24      0         5
76      84      0         8
6        55       0        12
代码如下:

$obj_db->simplequery("update member set ccd=cca+ccb");


这样得到的结果是:
cca   ccb    ccd        id
45      66    111          1
67      36      103         2
86      24      110         5
76      84      160         8
6        55       61        12
是我想要得到的结果。

但我还想将所有明细在另一表中记录下来,如果执行一条我会的:
$id=12;<br />$obj_db->simplequery("update member set ccd=cca+ccb  where id='$id'");<br /><br /><br />$q="select * from member where id='$id'";<br />$r=$obj_db->simplequery($q);<br />$a=$obj_db->fetchrow($r,DB_FETCHMODE_ASSOC);<br /><br />$mccd=$a[cca]+$a[ccb];<br /><br />$query="insert into mingxi (mid,mccd,mtime) values ('$id','$mccd','$time')";<br />$obj_db->simplequery($query);


得到:
mid    mccd   mtime             id
12     61     1394963723        1


但如何批量得到:
mid    mccd   mtime            id
1     111     1394963723        1
2     103     1394963723        2
5     110     1394963723        3
8     160     1394963723        4        
12     61     1394963723        5

请高手帮忙给出详细代码,谢谢!
------解决方案--------------------
//$id=12;<br />$obj_db->simplequery("update member set ccd=cca+ccb ");<br /> <br /> <br />$q="select * from member'";<br />$r=$obj_db->simplequery($q);<br />while($a=$obj_db->fetchrow($r,DB_FETCHMODE_ASSOC)){<br />   $id=$a[id];<br />   $mccd=$a[cca]+$a[ccb];<br />   <br />   $query="insert into mingxi (mid,mccd,mtime) values ('$id','$mccd','$time')";<br />   $obj_db->simplequery($query);<br />}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn