Count the total number of rows
mysql>
mysql> CREATE TABLE IF NOT EXISTS members
-> (
-> id INT AUTO_INCREMENT PRIMARY KEY,
-> name CHAR(10) NOT NULL,
-> email VARCHAR(30)
-> );
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> # insert 5 records into the "members" table
mysql> INSERT INTO members (name) VALUES ("database");
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO members (name, email) VALUES("Homer", "homer@mailserver.usa");
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO members (name) VALUES ("Marge");
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO members (name, email) VALUES("Bart", "bart@mailserver.usa");
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO members (name) VALUES ("Lisa");
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> SELECT COUNT(*) AS total_number_of_rows FROM members;
+----------------------+
| total_number_of_rows |
+----------------------+
| 5 |
+----------------------+
1 row in set (0.00 sec)
mysql>
mysql> SELECT COUNT(email) AS rows_with_email_addresses
-> FROM members;
+---------------------------+
| rows_with_email_addresses |
+---------------------------+
| 2 |
+---------------------------+
1 row in set (0.00 sec)
mysql>
mysql> # delete this sample table
mysql> DROP TABLE IF EXISTS members;
Query OK, 0 rows affected (0.00 sec)
Related examples in the same category