简体   繁体   中英

how to get total number of rows from mysql table?

I am writing code for getting total number of records from mysql table(storage engine: InnoDB) and show it on front-end. I am using COUNT(id) for this, like below:

SELECT COUNT(id) AS total_count FROM my_table_name

but I am getting 6 rows less of total count ie, I have 73 records totally but I am only getting 67 records as total count. I search for other solutions I found one solution like below:

SHOW TABLE STATUS FROM my_table_name;

but it is throwing mysql Access denied error like "Access denied for user 'my_user_name'@'localhost' to database 'my_table_name'". can anyone tell me how to achieve this. Thanks in advance.

are you sure your table has 73 records? or did you just look at the highest id? There could be some missing ids inbetween (removed). The query looks fine and it should work. please check again in phpMyAdmin

mysql count function will return all the rows if called with argument * like below

select count(*) as total from myTable

but in case if you provide any column name of your table like

 select count(id) as total from myTable

then the count of only those rows will be returned which id field is not NULL . So may be some of your rows have their id field NULL that's why the number of row returned by the query is less than actuall.

SELECT COUNT(*) AS total_count FROM my_table_name

也许并非所有行都有id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM