2012-09-08 40 views
1

可能重複:
Fastest way to count exact number of rows in a very large table?獲取的行數在MySQL

查詢什麼我用它來獲得具有共同規範的行數。
實施例:其中idpost = 3

+0

可能重複[?最快的方法來計算一個非常大的錶行的確切人數(http://stackoverflow.com/questions/6069237/fastest-way-to-count - 在一個非常大的表中的行數)或[php:統計mysql中的行數?](http://stackoverflow.com/questions/7049153/count-the-number -of-rows-in-mysql) –

回答

1

learning SQL考慮的行數:

select count(*) from mytable where idpost=3 
+0

請不要鏈接到w3schools,更多信息請參閱[W3Fools](http://w3fools.com/) –

+0

這是多麼糟糕!來自街道的隨機顛簸,寫一個HTML頁面! – JvdBerg

+0

不完全隨機的顛簸,如果您訪問該頁面,您會注意到作者列表。 –

6

可以使用COUNT()記錄here

SELECT COUNT(1) FROM posts WHERE idpost = 3 

編輯:根據dbf的建議更新..請確保您COUNT區分(*)和COUNT(1),討論here

+0

謝謝你soo:D –

+2

由於性能原因,如果表中存在很多字段,我會考慮使用COUNT(idpost)而不是*(全部) – dbf

+3

這個鏈接問題的接受答案(具體到[標記:sql-server],而不是[tag:mysql])和['COUNT()']的手動條目(http://dev.mysql.com/doc//en/group-by-functions.html# function_count)明確說明COUNT(*)是完全有效的。 MySQL手冊指出:「如果SELECT從一個表中檢索,並且沒有其他列被檢索,並且沒有WHERE子句,* * COUNT(*)'被優化爲非常快速地返回*」 – eggyal

1

嘗試

SELECT 
    COUNT(*) as NumberRows 
FROM 
    your_table_name_here 
WHERE 
    idpost = 3 
;