PHP / MySQL:递归数组/递归SELECT

时间:2015-07-25 07:50:51

标签: php mysql recursion

EXPLAIN

假设我们有transaction表:

+----+-------------+--------+----------------+----------------+---------------------+
| id | id_customer | amount | id_where_trans | id_money_from  |        date_trans   |
+----+-------------+--------+----------------+----------------+---------------------+
|  1 |          10 |     10 |            100 | NULL           | 2015-07-20 10:20:30 |
|  2 |          10 |     -2 |            100 | NULL           | 2015-07-21 09:10:11 |
|  3 |          10 |      7 |            120 | NULL           | 2015-07-24 18:22:25 |
|  4 |          10 |    -11 |            120 | here the magic | 2015-07-24 18:22:26 |
+----+-------------+--------+----------------+----------------+---------------------+

用“人类”语言阅读:

id 1 =>客户Alessandro(id_customer 10)在商店A的帐户中收取10欧元(id_where trans = 100)

id 2 =>客户Alessandro在A店花了2欧元

id 3 =>客户亚历山德罗在商店 B

的帐户中收取7欧元的费用

id 4 =>顾客ALessandro在B店花11欧元。

在期末(例如月份),商店B需要从我们的示例商店A中的另一家商店获得8欧元(+ 10-2)。

/人类阅读结束。

你想象插入是一个简单的(例如id 4):

INSERT INTO transaction (id_customer, amount, id_where_trans, date) VALUES (10,-11,120,2015-07-24 18:22:26)

目标

我的目标是设置 - 在插入期间,如果可能/通过PHP更简单 - SQL分为两个或更多SELECT:

INSERT INTO transaction (id_customer, amount, id_where_trans, id_money_from, date) VALUES (10,-8,120,100,2015-07-24 18:22:26)

INSERT INTO transaction (id_customer, amount, id_where_trans, id_money_from, date) VALUES (10,-3,120,NULL,2015-07-24 18:22:27)



    +----+-------------+--------+----------------+------------+---------------------+
| id | id_customer | amount | id_where_trans | id_money_from |        date         |
+----+-------------+--------+----------------+------------+---------------------+
|  1 |          10 |     10 |            100 | NULL       | 2015-07-20 10:20:30 |
|  2 |          10 |     -2 |            100 | NULL       | 2015-07-21 09:10:11 |
|  3 |          10 |      7 |            120 | NULL       | 2015-07-24 18:22:25 |
|  4 |          10 |     -8 |            120 | 100        | 2015-07-24 18:22:26 |
|  5 |          10 |     -3 |            120 | NULL       | 2015-07-24 18:22:27 |
+----+-------------+--------+----------------+------------+---------------------+

如果我可以获得包含该信息的表格,我可以运行一个查询,使我的最终工作正确。

基本上,我需要计算用于放电的货币的id_shop(在这种情况下,从100开始的-8,先前的充电,因此是非空,并且来自120的-3,本身,这是空的)

请注意,我首先消耗/解除之前的费用(id 4现在有id_money_from 100),之后我会消耗/解除其他金额(id 5实际上是NULL,因为-3取自了id 3)

假设/强制

1)id是INCREMENTAL,没有并发。日期为INCREMENTAL,id 4的日期为> = id为3,依此类推。

2)如果上次充值的数量来自完成交易的相同id(参见id 1和id 2),则插入NULL(我需要mandatary NULL)

3)首次充电,首先需要归零,依此类推。

MY LOGIC / PSEUDOCODE

1)如果交易是否定的,则递归获得最后的正电荷,其总和为负的,不是零。

得到这笔费用,如果id_where_trans == id_money_from我们需要插入,只需插入,带NULL

INSERT INTO transaction (id_customer, amount, id_where_trans, id_money_from, date) VALUES (10,-2,120,NULL,2015-07-24 18:22:26)

从这里递归开始

如果这个数量是< =最后一次充值,走进数据库并在两个或更多 id_where_trans中拆分为负(实际情况中的问题,这将是不可能的,但我需要思考出院时的金额(id_money_from)可以分为2,3,x id_where_trans)。例如,如果SHOP A充电+1,B铺充电+1,车间C充电+1和SHOP D DISCHARGE -3,我们需要插入3个不同的行。

$amount_to_discharge = x;

$last_positive = $query->("SELECT * FROM transaction WHERE "); // make the SUB-SUM

if ($last_positive['amount'] >= $amount_to_discharge) {
    $query->("INSERT INTO......");
} else {
   // start recursive
   $sql = "SELECT * FROM AMOUNT WHERE amount > 0 AND id < $last_positive['id']";
   $new_search = $query->sql($sql);
   // how implement correctly that recursive?

}
谢谢你。如果您需要其他人解释,请告诉我们!

0 个答案:

没有答案