0

有问题的查询是

SELECT MAX(Date) as max, MIN(Date) as min FROM 
table 
WHERE Id = 'id' 
ORDER BY Date DESC 
LIMIT 100

我想要它做的是返回最近 100 条记录的最大和最小日期。它的作用是显示所有记录的最小值和最大值。

4

3 回答 3

2

您必须首先选择您感兴趣的记录,然后从中选择max()min()

SELECT max(Date) AS max,
       min(Date) AS min
  FROM ( SELECT *
           FROM t
          WHERE Id = 'id'
       ORDER BY Date DESC
          LIMIT 100) x;
于 2012-08-10T08:29:44.143 回答
2

不要忘记用反引号转义列。

SELECT max(`date`) AS maxDate,
       min(`date`) AS minDate
FROM  ( 
         SELECT `date`
         FROM   myTable
         WHERE Id = 'id'
         ORDER BY `date` DESC
         LIMIT 100
      )
于 2012-08-10T08:37:54.123 回答
1
select min(Date) as min, max(Date) as max from
(
     SELECT Date FROM your_table 
     WHERE Id = 'id' 
     ORDER BY Date DESC  
     LIMIT 100
) x
于 2012-08-10T08:29:18.277 回答