mysql 在一个范围内搜索最小(日期)

mysql search min(date) within a range

我想在设定范围内找到最短日期,下面的示例我想找到每个学生的最短申请日期。 MySQL 不接受此查询。

SELECT MIN(applications.date) AS 'D', students.name
FROM applications, students
WHERE applications.student_id = students.id AND
D BETWEEN '2016-05-01' AND '2016-05-31'
GROUP BY students.id

试试这个,只需在 where 子句中使用 table 列而不是别名;)

SELECT *
FROM (
    SELECT MIN(applications.date) AS 'D', students.name
    FROM applications, students
    WHERE applications.student_id = students.id
    GROUP BY students.id
) tmp
WHERE D BETWEEN '2016-05-01' AND '2016-05-31'

您不能使用 where 子句对 min() 聚合函数的结果应用过滤器,而是可以使用 having 子句

SELECT MIN(a.date) AS `D`,s.id,s.name
FROM applications a
JOIN students s ON a.student_id = s.id 
GROUP BY s.id,s.name
HAVING `D` >= '2016-05-01' AND `D` <= '2016-05-31'