说我在MySQL表中有50行。我想选择前十个(LIMIT 10
),但我希望能够在另一个页面上选择下一个10。
那么如何在第10行之后开始我的选择?
更新了查询:
mysql_query("
SELECT * FROM `picdb`
WHERE `username` = '$username'
ORDER BY `picid` DESC
LIMIT '$start','$count'
")
答案 0 :(得分:63)
我建议您使用以下方式获取第一页:
LIMIT 0, 10
然后是第二页
LIMIT 10, 10
然后
LIMIT 20, 10
表示第三页,依此类推。
答案 1 :(得分:28)
LIMIT 10
LIMIT 10 OFFSET 10
来自MySQL 5.1 docs on SELECT
syntax:
为了与PostgreSQL兼容, MySQL也支持LIMIT row_count OFFSET偏移语法。
答案 2 :(得分:1)
这个问题很老但我只是想添加一个非硬编码的代码,答案混乱就意味着你必须硬编码你的脚本(Select语句)。您可以通过获取文件名然后根据当前页面从数据库中选择数据来获得相同的结果,而无需对select语句进行硬编码。 首先获取当前页面
$page = basename($_SERVER['SCRIPT_FILENAME']);
$page_counter = rtrim($page, ".php");
//setting your limit
$start = 0;
$limit = 10;
//if current page is not index.php then $start = ($limit * page_counter);
// e.g if current page is 1.php then $start = ($limit * 1) = 10
//if current page is 2.php then $start = ($limit * 2) = 20
if ($page !== 'index.php') {
$start = ($limit * $page_counter);
}
//getting row count
$ROW_COUNT = $db->query('SELECT * from tableName')->rowCount();
//getting number of rows left in the table
$rows_left = ("SELECT * FROM tableName limit ?,?");
$rows_left = $db->prepare($rows_left);
$rows_left->execute(array($start,$ROW_COUNT));
$rows = $rows_left->fetchAll(PDO::FETCH_ASSOC);
$number_rows = 0;
foreach ($rows as $r) {
$number_rows = $number_rows + 1;
}
//if number of rows left in the table is less than 10 then $limit = the number of rows left
if ($number_rows < 10) {
$limit = $number_rows;
}
//getting all rows
$getRows = "SELECT * FROM tableName limit ?,?";
$getRows = $db->prepare($getRows);
$getRows->execute(array($start , $limit));
$getRows = $getRows->fetchAll(PDO::FETCH_ASSOC);
答案 3 :(得分:0)
select * from 'table_name'
ORDER BY 'column_id 'DESC
LIMIT 0,10;
select * from 'table_name'
ORDER BY 'column_id' DESC
LIMIT 10,10;
select * from 'table_name'
ORDER BY 'column_id' DESC
LIMIT 20,10;
并继续直到你想要的数字。