gpt4 book ai didi

两张表之间的Mysql操作,并希望在一张表中输出

转载 作者:行者123 更新时间:2023-11-28 23:19:56 25 4
gpt4 key购买 nike

我在 amount_to_pay 表数据中有两个表 amount_to_paypaid_amount 根据 center_id 插入university_id 他为哪个大学的哪个中心支付了多少费用,并在 paid_amount 数据中插入了他为哪个大学的哪个中心支付了多少费用,我希望中心的 Netty 待定具体大学如下面第三张表所示。

这是第一个表格amount_to_pay

+---------+--------------+-------------+
|center_id|university_id |amount_topay |
+---------+--------------+-------------+
| 1 | 6 |29000 |
+---------+--------------+-------------+
| 1 | 7 |19700 |
+---------+--------------+-------------+
| 2 | 6 |9000 |
+---------+--------------+-------------+

这是我的第二张表 paid_amount

+---------+--------------+------------+
|center_id|university_id |amount_paid |
+---------+--------------+------------+
| 1 | 6 |9000 |
+---------+--------------+------------+
| 2 | 6 |5000 |
+---------+--------------+------------+

并希望输出如下表

+---------+--------------+-------+
|center_id|university_id |amount |
+---------+--------------+-------+
| 1 | 6 |20000 |
+---------+--------------+-------+
| 1 | 7 |19700 |
+---------+--------------+-------+
| 2 | 6 |4000 |
+---------+--------------+-------+

上面的 amount_topay 列是 amount_topay 和按 center_id 和 university_id 分组的总和在第二个表中,amount_paid 也是 amount_paid 列和按 center_id 和 university_id 分组的总和。

最佳答案

您可以连接表格并减去金额。请注意,您应该使用 left join,这样您就不会错过尚未付款的条目:

SELECT    a.center_id,
a.university_id,
a.amount_to_pay - COALESCE(p.amount_paid, 0) AS amount
FROM amount_to_pay a
LEFT JOIN paid_amount p ON a.center_id = p.center_id AND
a.university_id = p.university_id

关于两张表之间的Mysql操作,并希望在一张表中输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42173592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com