最好,最简单的方法是什么?我的查询目前是:
SELECT *
FROM chat
WHERE (userID = $session AND toID = $friendID)
OR (userID = $friendID AND toID = $session)
ORDER BY id
LIMIT 10
这显示前10行,而不是最后10行。
编辑:我想要最后10行(是的,DESC这样做)但是我希望它们以ASCENDING顺序返回。
答案 0 :(得分:24)
要撤销订单(因此获得最后10个而不是前10个),请使用DESC
代替ASC
修改强>
根据您的评论:
SELECT * FROM (
SELECT *
FROM chat
WHERE (userID = $session AND toID = $friendID)
OR (userID = $friendID AND toID = $session)
ORDER BY id DESC
LIMIT 10
) AS `table` ORDER by id ASC
答案 1 :(得分:1)
如果你想要最后10个,那么只需将ASC更改为DESC
SELECT *
FROM
chat
WHERE
(userID=$session AND toID=$friendID)
OR
(userID=$friendID AND toID=$session)
ORDER BY id
DESC
LIMIT 10
答案 2 :(得分:0)
$con = mysqli_connect("localhost","my_user","my_password","my_db");
$limit = 10;
$query = "SELECT * FROM $table";
$resource = mysqli_query($con,$query);
$total_rows = mysqli_num_rows($resource);
$start = $total_rows-$limit;
$query_limit= $query." LIMIT $start,$limit";
首先我设定了限制
$limit = 10;
然后
$total_rows = mysqli_num_rows($resource);
这里我采取了受影响的总行数。
$start = $total_rows-$limit;
然后从行数减去限制以获取起始记录号
$query_limit= $query." LIMIT $start,$limit";
然后为查询添加限制。 有关限制的更多信息,请参阅此链接 https://www.w3schools.com/php/php_mysql_select_limit.asp
答案 3 :(得分:0)
首先从表格中选择最后10个,然后以升序对其重新排序。
SELECT * FROM (SELECT * FROM table ORDER BY id DESC LIMIT 10) sub ORDER BY id ASC