Computer >> 컴퓨터 >  >> 프로그램 작성 >> MySQL

MySQL 데이터베이스의 모든 행의 정확한 수는?

<시간/>

모든 행을 정확히 계산하려면 집계 함수 COUNT(*)를 사용해야 합니다. 구문은 다음과 같습니다 -

select count(*) as anyAliasName from yourTableName;

위의 구문을 이해하기 위해 테이블을 생성해 보겠습니다. 테이블을 생성하는 쿼리는 다음과 같습니다 -

mysql> create table CountAllRowsDemo
   -> (
   -> Id int,
   -> Name varchar(10),
   -> Age int
   -> );
Query OK, 0 rows affected (1.49 sec)

이제 insert 명령을 사용하여 테이블에 일부 레코드를 삽입할 수 있습니다. 쿼리는 다음과 같습니다 -

mysql> insert into CountAllRowsDemo values(1,'John',23);
Query OK, 1 row affected (0.15 sec)
mysql> insert into CountAllRowsDemo values(101,'Carol',21);
Query OK, 1 row affected (0.17 sec)
mysql> insert into CountAllRowsDemo values(201,'Sam',24);
Query OK, 1 row affected (0.13 sec)
mysql> insert into CountAllRowsDemo values(106,'Mike',26);
Query OK, 1 row affected (0.22 sec)
mysql> insert into CountAllRowsDemo values(290,'Bob',25);
Query OK, 1 row affected (0.16 sec)
mysql> insert into CountAllRowsDemo values(500,'David',27);
Query OK, 1 row affected (0.16 sec)
mysql> insert into CountAllRowsDemo values(500,'David',27);
Query OK, 1 row affected (0.19 sec)
mysql> insert into CountAllRowsDemo values(NULL,NULL,NULL);
Query OK, 1 row affected (0.23 sec)
mysql> insert into CountAllRowsDemo values(NULL,NULL,NULL);
Query OK, 1 row affected (0.13 sec)

select 문을 사용하여 테이블의 모든 레코드를 표시합니다. 쿼리는 다음과 같습니다 -

mysql> select *from CountAllRowsDemo;

다음은 출력입니다 -

+------+-------+------+
| Id   | Name  | Age  |
+------+-------+------+
|    1 | John  | 23   |
|  101 | Carol | 21   |
|  201 | Sam   | 24   |
|  106 | Mike  | 26   |
|  290 | Bob   | 25   |
|  500 | David | 27   |
|  500 | David | 27   |
| NULL | NULL  | NULL |
| NULL | NULL  | NULL |
+------+-------+------+
9 rows in set (0.00 sec)

집계 함수 count(*)를 사용하여 테이블의 정확한 행 수를 계산하는 방법은 다음과 같습니다.

쿼리는 다음과 같습니다 -

mysql> select count(*) as TotalNumberOfRows from CountAllRowsDemo;

다음은 행 수가 포함된 출력입니다. -

+-------------------+
| TotalNumberOfRows |
+-------------------+
|                 9 |
+-------------------+
1 row in set (0.00 sec)