0

我正在尝试在我的奖励系统中列出前 20 名收入者的列表。

以下陈述完美地表明:

SELECT
    S.User_ID As UserID,
    CONCAT(S.User_Forename, " ", S.User_Surname) AS StudentName,
    T.TotalPoints
FROM
    student S
INNER JOIN 
    ( SELECT
        Datetime, Recipient_ID, SUM(Points) AS TotalPoints
      FROM
        transactions
      GROUP BY
        Recipient_ID ) T
ON
    S.User_ID = T.Recipient_ID
ORDER BY TotalPoints DESC
LIMIT 20

然而,想想看,明年,我需要包括一个日期范围(即仅显示该学年的最高收入者)。

出于测试目的,我尝试修改以下包含LESS THAN DATETIME选择的语句:

SELECT
    S.User_ID As UserID,
    CONCAT(S.User_Forename, " ", S.User_Surname) AS StudentName,
    T.TotalPoints
FROM
    student S
INNER JOIN 
    ( SELECT
        Datetime, Recipient_ID, SUM(Points) AS TotalPoints
      FROM
        transactions
      GROUP BY
        Recipient_ID ) T
ON
    S.User_ID = T.Recipient_ID
WHERE t.Datetime < 2012-03-20
ORDER BY TotalPoints DESC
LIMIT 20

我也试过这个:

SELECT
    S.User_ID As UserID,
    CONCAT(S.User_Forename, " ", S.User_Surname) AS StudentName,
    T.TotalPoints
FROM
    student S
INNER JOIN 
    ( SELECT
        Datetime, Recipient_ID, SUM(Points) AS TotalPoints
      FROM
        transactions
      WHERE Datetime < 2012-03-20
      GROUP BY
        Recipient_ID ) T
ON
    S.User_ID = T.Recipient_ID
ORDER BY TotalPoints DESC
LIMIT 20

然而,这两个都显示一个空的结果集。

如果我做一个简单的SELECT * FROM transactions WHERE Datetime < 2012-03-20,将返回超过 25K 的结果。

我哪里错了?

4

1 回答 1

1

'您的日期值周围似乎缺少单引号:

WHERE t.Datetime < '2012-03-20'
于 2012-07-09T15:50:32.303 回答