[英]mysql - how to count how many records there are in a table and then only get 20?
我正在为一个学校项目建立一个论坛,我只想为一个论坛每页显示20条帖子。 但是,我仍然需要知道总共有多少帖子才能显示页面列表。 我可以运行一个mysql查询,将帖子数限制为20,然后运行另一个查询,该查询将计算表中有多少条记录,但这将是2条查询。 难道没有办法在同一查询中同时获得有限数量的记录并获得总数的记录吗?
谢谢。
您可以使用子查询
select *,
(select count(*) from your_table) as total_count
from your_table
order by some_column
limit 20
我在扩大juergen的职位:
$result = mysql_query("SELECT * (SELECT COUNT(*) FROM table) AS total FROM tablePRDER BY id LIMIT 0, 20");
while($row = mysql_fetch_assoc($result))
{
$total = $row['total']; /* Total rows of the whole table */
$id = $row['id']; /* Id of limitted query (20) */
}
select *
from your_table
inner join (select count(*) as total_count from your_table) tc
order by some_column
limit 20;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.