0

表 1:用户管理员

uid uname
1    abc
2    xyz
3    pqr
4    def

表 2:事务主管

tid uid amount type
1    1  100    1
2    2  500    1
2    2  500    2
3    1  350    1
3    1  150    2

输入事务表:

1 for capital
2 for interest(5% of total capital)

现在,我想计算interest每个月的资本金额和资本价值 5% 的广告利息。应通过查询:自动为有资金的两个用户在表中添加利息条目transactionmaster

transactionmaster结果在表中应该是这样的。

tid uid amount type
1    1  100    1
2    2  500    1
3    1  600    1
4    1  35     2
5    2  25     2

这里interest也自动算5%。

4

2 回答 2

0

要在每个月自动获取结果,您需要使用 MySQL 事件安排 SQL 查询。

这是参考 1) http://www.infotuts.com/schedule-sql-query-using-phpmyadmin-mysql-events/

从 transactionmaster 获取资金总和

select sum(amount) from transactionmaster where uid = 13 and type=1

现在计算利息

select sum(amount) * (5 / 100)  as interest from transactionmaster where uid=13 and type=1

简单的!

于 2017-02-09T12:36:30.777 回答
0

像这样的东西应该可以解决问题:

INSERT INTO transactionmaster (uid, amount, type)
SELECT uid, ((SUM(amount) / 100) * 5), 2
FROM transactionmaster
WHERE type = 1 
GROUP BY uid

我假设该tid领域是一个自增


编辑:上面的查询是一次性的。即,它将系统地为所有具有“类型 1”的 uid 创建“类型 2”条目。换句话说,如果你多次使用它,你最终会得到重复的“type 2”条目。

如果您只想为没有“type 2”行的“type 1”插入“type 2”行,您可以这样做:

INSERT INTO transactionmaster (uid, amount, type)
SELECT t1.uid, ((SUM(t1.amount) / 100) * 5), 2
FROM transactionmaster t1 
LEFT JOIN transactionmaster t2 ON t1.uid=t2.uid AND t1.type=1 AND t2.type=2 
WHERE t2.tid IS NULL
GROUP BY t1.uid

编辑 2 以回答您的评论。

假设您创建了一个具有以下结构的 intrustmaster 表:

loweramt | higheramt | perc
---------------------------
100      | 199       | 5
200      | 399       | 4

oneshot 查询将变为:

INSERT INTO transactionmaster (uid, amount, type)
SELECT T.uid, ((totamt /100) * i.perc), 2
FROM 
(
    SELECT uid, (SUM(amount) / 100) as totamt
    FROM transactionmaster
    WHERE type = 1 
    GROUP BY uid
) T
INNER JOIN intrustmaster I
  ON t.totamt BETWEEN i.loweramt AND i.higheramt
于 2017-02-09T12:34:43.650 回答