gpt4 book ai didi

Mysql子查询问题

转载 作者:行者123 更新时间:2023-11-30 23:40:21 25 4
gpt4 key购买 nike

  • 贷款(列:id,用户)
  • 返回(列:id、installment、scheduleDate)

如果一笔贷款要分12期还清,则有12 strip 贷款id的还款记录。

我想找出哪些用户的 scheduleDate 的所有返回之和大于 2000。

SELECT user 
FROM {loans} ka
WHERE
(
SELECT MAX(inst) FROM
(
SELECT SUM(installment) AS inst
FROM {payback}
WHERE id IN
(
SELECT id
FROM {loans}
WHERE user = ka.user
)
GROUP BY scheduleDate
) as t1 LIMIT 0,1
) > 2000

我收到错误:“where 子句”中的未知列“ka.user”

最佳答案

这是否按要求工作?它假定 payback.id 是引用 loads.id 的外键。

SELECT
user, scheduleDate, sumInstallments
FROM
(
SELECT
l.user, pb.scheduleDate, SUM(pb.installment) AS sumInstallments
FROM
payback AS pb
JOIN
loans AS l
ON
l.id = pb.id
GROUP BY
l.user, pb.scheduleDate
) AS tempId
WHERE
sumInstallments > 2000

关于Mysql子查询问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3850030/

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