简体   繁体   English

如何从mysql表中获取总行数?

[英]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. 我正在编写用于从mysql表(存储引擎:InnoDB)中获取记录总数的代码,并将其显示在前端。 I am using COUNT(id) for this, like below: 我为此使用COUNT(id),如下所示:

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. 但是我的总计数减少了6行,即,我总共有73条记录,但总计数却只有67条记录。 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'". 但是它会抛出mysql访问被拒绝的错误,例如“用户'my_user_name'@'localhost'对数据库'my_table_name'的访问被拒绝”。 can anyone tell me how to achieve this. 谁能告诉我如何实现这一目标。 Thanks in advance. 提前致谢。

are you sure your table has 73 records? 您确定您的表有73条记录吗? or did you just look at the highest id? 还是只看了最高的ID? There could be some missing ids inbetween (removed). 中间可能有一些丢失的ID(已删除)。 The query looks fine and it should work. 该查询看起来很好,它应该可以工作。 please check again in phpMyAdmin 请在phpMyAdmin中再次检查

mysql count function will return all the rows if called with argument * like below 如果使用参数*调用,mysql count函数将返回所有行,如下所示

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 . 那么只会返回id字段不为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. 因此,您的某些行的ID字段可能为NULL ,这就是查询返回的行数少于实际的原因。

SELECT COUNT(*) AS total_count FROM my_table_name

也许并非所有行都有id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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