gpt4 book ai didi

SQL语法问题

转载 作者:行者123 更新时间:2023-11-29 04:31:09 25 4
gpt4 key购买 nike

有关 UPDATE 的 SQL 语法问题。通过给出以下示例来提出问题会容易得多:

**Payments Table**

ID user_id payment_due payment_made
1 3 10.0 5.0
1 3 10.0 10.0
1 9 20.0 20.0



**Balance Table**

ID user_id current_balance
1 3 ???
2 9 ???

假设我想更新特定用户的当前余额。什么是正确有效的 SQL 语法来添加所有付款费用并从为特定用户支付的所有付款中减去它?

在这种情况下,用户“3”的 current_balance 为 5.0,用户“9”的 current_balance 为 0.0

最佳答案

怎么样:

select
ID,
user_id,
(sum(payment_due) - sum(payment_made)) current_balance
from
PaymentsTable

group by
ID,
user_id

关于SQL语法问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2373939/

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