count number of rows in mysql code example

Example 1: mysql get count of rows

SELECT COUNT(*) FROM my_table;
SELECT COUNT(*) FROM my_table WHERE my_column IS NULL;
SELECT COUNT(*) FROM my_table WHERE my_column = 'my_value';
SELECT COUNT(DISTINCT tel) gender_count,
       COUNT(DISTINCT CASE WHEN gender = 'male'   THEN tel END) male_count,
       COUNT(DISTINCT CASE WHEN gender = 'female' THEN tel END) female_count
FROM people;

Example 2: get number of rows in every table mysql

mysql> SELECT table_name, table_rows
   ->FROM INFORMATION_SCHEMA.TABLES
   ->WHERE TABLE_SCHEMA = 'business';

Example 3: mysql count table rows

select table_name, sum(table_rows) as sum 
from information_schema.tables
where table_schema = '[DB NAME]'
group by table_name 
order by sum desc;

Tags:

Php Example