我有一个数据库,由id
从1开始排序。
我还为每条记录设置了时间戳顺序'stamptime'
我尝试返回id=10
和id=15
之间的最大'stamptime'
。我尝试了这个代码,但没有成功…
$sql = "SELECT MAX(stamptime) as max, id FROM Articls ORDER BY id DESC LIMIT 10, 15";
$result = $conn->query($sql);
$row = mysqli_fetch_array($result);
$this->disconnectDB($conn);
return $row['0'];
正确的做法是什么?
limit
https://dev.mysql.com/doc/refman/5.0/en/select.html表示要返回多少行。您想使用between
, https://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_between.
SELECT MAX(stamptime) as max, id FROM Articls where id between 10 and 15
您当前的代码说选择将作为第1行返回的最大时间戳。该命令从第11行开始返回,并返回15行,这些行都是空的。从限制文档中,the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return
应该可以:
SELECT ID, MAX(STAMPTIME) AS MAX FROM ARTICLS WHERE ID>9 AND ID<16 ORDER BY ID DESC
您只能将order by
子句与desc
一起使用,然后再使用limit 1
select
stamptime,
id
from Articls
where id between 10 and 15
order by stamptime desc limit 1