我有一个由id
从1开始排序的数据库。
我还为每条记录设置了'stamptime'
我尝试在'stamptime'
和id=10
之间返回最大id=15
。我试过这段代码却没有成功......
$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'];
这样做的正确方法是什么?
答案 0 :(得分:1)
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
答案 1 :(得分:0)
这应该有效:
SELECT ID, MAX(STAMPTIME) AS MAX FROM ARTICLS WHERE ID>9 AND ID<16 ORDER BY ID DESC
答案 2 :(得分:0)
您只能order by
使用desc
,然后使用limit 1
select
stamptime,
id
from Articls
where id between 10 and 15
order by stamptime desc limit 1