剔除第一张表的数据时,修改第二张表的相关字段的数值

WBOY
Release: 2016-06-13 12:01:04
Original
1335 people have browsed it

删除第一张表的数据时,修改第二张表的相关字段的数值
表xf
xf_id       vip        total
1            1           10
2            1           100
3            2           80
4            3           50


表vip
vip          jifen
 1          1000
 2           500
 3           800

$sql = "delete from " . $fdyu->table('xf') .
          " WHERE xf_id " . db_create_in(join(',', $_POST['checkboxes'])) .    //假如$_POST['checkboxes']就是xf_id:1,2,3,4
           " AND school_id=" . $school_id;
          $db->query($sql);

那么在删除xf_id的同时,表vip里的
1   jifen=1000-110=890     如果xf里有多个一样的vip,则减去总和,如xf表里vip为1的有两条数据,他们的total和是110,
                          则减110
2   jifen=500-80=420
3   jifen=800-50=750
也就是说在删除表xf 里的数据的同时,表vip里的jifen要减去xf表里的total
------解决方案--------------------
大概思路

1.先获取要删除记录的vip,和每个vip共删除的总分
select vip,sum(total) from xf where xf_id in(1,2,3,4) group by vip;
保存为数组
vip为key
total为value

$arr = array(
'1' => 110,
'2' => 80,
'3' => 50
);

2.删除对应xf_id的记录
delete from xf where xf_id in(1,2,3,4);

3.修改vip表
foreach($arr as $vip=>$total){
update vip set jifen=jifen-$total where vip=$vip
}

------解决方案--------------------
可以考虑用触发器,在MYSQL执行,给你一个例子:

<br />create   trigger   delete_jifen   before delete   on   jifen<br />  for   each   row  <br />BEGIN<br />UPDATE users  SET users.sum = users.sum - <br />		(SELECT sum(jifen.jifen) from jifen WHERE jid = old.jid) where users.userid = old.userid;<br />END<br />
Copy after login

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template