返回mysqli / php中2个字段范围之间的最大值

时间:2015-05-28 15:46:39

标签: php mysql

我有一个由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'];

这样做的正确方法是什么?

3 个答案:

答案 0 :(得分:1)

limithttps://dev.mysql.com/doc/refman/5.0/en/select.html用于返回多少行。您想使用betweenhttps://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