如何根据 sum() 的条件更新值?

How can I update value with the conditions of sum()?

我有一个 tables(以下银行模式)如下:

//account
+----------------+---------+
| account_number | balance |
+----------------+---------+
| 11_01          |     309 |
| 11_02          |     515 |
| 12_01          |    1050 |
| 13_01          |  105000 |
| 13_02          |    1000 |
| 14_01          |    6300 |
| 17_01          |   10500 |
+----------------+---------+
//customer
+-------------+---------------+--------------------+---------------+---------------+
| customer_id | customer_name | customer_street    | customer_city | customer_type |
+-------------+---------------+--------------------+---------------+---------------+
| abc         | 이승종        | 송파구 방이동 28-2 | 서울          | NULL          |
| def         | 이규현        | 남구 달동 1251-5   | 울산          | NULL          |
| ghi         | 이명구        | 동래구 수안동 2-5  | 부산          | NULL          |
| jkl         | 신영찬        | 신곡동 357         | 의정부        | NULL          |
| mno         | 김민석        | 신장동 444-25      | 하남          | NULL          |
| pqr         | 박승원        | 북구 삼각동 592-1  | 광주          | NULL          |
| stu         | 정창현        | 서구 청라동 156-1  | 인천          | NULL          |
+-------------+---------------+--------------------+---------------+---------------+
//depositor
+-------------+----------------+-------------+
| customer_id | account_number | access_date |
+-------------+----------------+-------------+
| abc         | 11_01          | 2021-11-12  |
| abc         | 11_02          | 2021-11-12  |
| def         | 12_01          | 2021-11-12  |
| ghi         | 13_01          | 2021-11-12  |
| ghi         | 13_02          | 2021-11-12  |
| jkl         | 14_01          | 2021-11-12  |
| stu         | 17_01          | 2021-11-12  |
+-------------+----------------+-------------+

对不起韩语,但不关心那个。

我想将客户 table 的列 customer_type 值更新为 'VIP',条件是每个客户的账户余额总和超过 10000。

我尝试了 inner join -> group by(customer_name) 子句,但效果不佳。 我应该使用什么子句?

UPDATE customer
LEFT JOIN ( SELECT customer_id, SUM(balance) > 10000 is_VIP
            FROM account
            JOIN depositor USING (account_number)
            GROUP BY customer_id ) check_VIP USING (customer_id)
SET customer_type = COALESCE(is_VIP, 0)
-- WHERE customer_type IS NULL;
如果客户是 VIP,

customer_type 将分配给 1,否则分配给 0。如果您需要设置另一个值,则根据 SET 子句中的 CASE 应用。例如 SET customer_type = CASE WHEN is_VIP THEN 'VIP' ELSE '' END.

如果此列在某些行中包含值并且不应更新这些行,请取消注释 WHERE。

您可以尝试在 join 中使用更新和 sum

的子查询
update customer c
inner join  (
    select d.customer_id, sum(a.balance) cust_sum
    from depositor d 
    inner join account a on a.account_number = d.account_number 
    group by d.customer_id
) t on t.customer_id = c.customer_id 
        AND t.cust:sum > 10000
set customer_type= 'VIP'