将日期合并到此GROUP BY和INNER JOIN选择语句中

时间:2012-07-09 15:40:32

标签: mysql

我正在努力制作奖励系统中前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的结果。

我哪里错了?

1 个答案:

答案 0 :(得分:1)

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

WHERE t.Datetime < '2012-03-20'