有问题的查询是
SELECT MAX(Date) as max, MIN(Date) as min FROM
table
WHERE Id = 'id'
ORDER BY Date DESC
LIMIT 100
我想要它做的是返回最近 100 条记录的最大和最小日期。它的作用是显示所有记录的最小值和最大值。
有问题的查询是
SELECT MAX(Date) as max, MIN(Date) as min FROM
table
WHERE Id = 'id'
ORDER BY Date DESC
LIMIT 100
我想要它做的是返回最近 100 条记录的最大和最小日期。它的作用是显示所有记录的最小值和最大值。
您必须首先选择您感兴趣的记录,然后从中选择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;
不要忘记用反引号转义列。
SELECT max(`date`) AS maxDate,
min(`date`) AS minDate
FROM (
SELECT `date`
FROM myTable
WHERE Id = 'id'
ORDER BY `date` DESC
LIMIT 100
)
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