簡體   English   中英

mysql-如何計算一個表中有多少條記錄,然后只得到20條?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM