1
  • 贷款(列:id,用户)
  • 回报(列:id、分期付款、scheduleDate)

如果要分 12 期偿还贷款,则有 12 条带有贷款 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”

4

1 回答 1

0

这是否按要求工作?它假设 payback.id 是一个外键,引用 load.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
于 2010-10-03T15:12:10.540 回答