我想知道如何在不使用while循环进行mysql查询的情况下获取最后一行值。
选择数据:
$sql = "
SELECT *
FROM status
WHERE author = '$pname'
AND postdate >= '$lsposttime'
ORDER
BY postdate ASC
LIMIT 4";
$query = mysqli_query($conn, $sql);
我希望在不使用循环的情况下获得第4行的postdate值。
不使用此:
while ($row = mysqli_fetch_assoc($query)){
}
答案 0 :(得分:2)
select *
from status
where author = '$pname'
and postdate >= '$lsposttime'
order by postdate asc LIMIT 3, 1
这将获得第四行
答案 1 :(得分:1)
You can use a query like this:
SELECT *
FROM (
SELECT *
FROM STATUS
WHERE author='$pname'
AND postdate >= '$lsposttime'
ORDER BY postdate ASC LIMIT 4
) tnp
ORDER BY postdate DESC LIMIT 1;
This will return the last row. So if the result only return 2 row it will give the second.
答案 2 :(得分:0)
您可以在mysql中使用偏移来从确切位置获取数据。
对于您的要求,您的查询将如下所示:
$sql = "
SELECT *
FROM status
WHERE author = '$pname'
AND postdate >= '$lsposttime'
ORDER
BY postdate ASC
LIMIT 1 OFFSET 4";
$query = mysqli_query($conn, $sql);
为了更多地使用偏移量,您可以从here
中发现