집계 COUNT(*)와 함께 하위 쿼리를 사용하여 총 행 수를 얻을 수 있습니다.
먼저 테이블을 생성하겠습니다 -
mysql> create table DemoTable ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentFirstName varchar(20), StudentAge int ); Query OK, 0 rows affected (0.48 sec)
삽입 명령을 사용하여 테이블에 일부 레코드 삽입 -
mysql> insert into DemoTable(StudentFirstName,StudentAge) values('John',23); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(StudentFirstName,StudentAge) values('Larry',21); Query OK, 1 row affected (0.40 sec) mysql> insert into DemoTable(StudentFirstName,StudentAge) values('Johnny',23); Query OK, 1 row affected (0.43 sec) mysql> insert into DemoTable(StudentFirstName,StudentAge) values('Chris',21); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(StudentFirstName,StudentAge) values('David',23); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(StudentFirstName,StudentAge) values('James',21); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(StudentFirstName,StudentAge) values('Jace',24); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(StudentFirstName,StudentAge) values('Mike',21); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(StudentFirstName,StudentAge) values('Robert',25); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(StudentFirstName,StudentAge) values('Mike',22); Query OK, 1 row affected (0.23 sec)
다음은 select 문을 사용하여 테이블의 모든 레코드를 표시하는 쿼리입니다 -
mysql> select *from DemoTable;
이것은 다음과 같은 출력을 생성합니다 -
+-----------+------------------+------------+ | StudentId | StudentFirstName | StudentAge | +-----------+------------------+------------+ | 1 | John | 23 | | 2 | Larry | 21 | | 3 | Johnny | 23 | | 4 | Chris | 21 | | 5 | David | 23 | | 6 | James | 21 | | 7 | Jace | 24 | | 8 | Mike | 21 | | 9 | Robert | 25 | | 10 | Mike | 22 | +-----------+------------------+------------+ 10 rows in set (0.00 sec)
다음은 MySQL 쿼리에 대한 총 행 수를 가져오고 모든 열 값에 대해 설정하는 쿼리입니다 -
mysql> select StudentFirstName,(select count(*) from DemoTable) AS Total_Count from DemoTable limit 10;
이것은 다음과 같은 출력을 생성합니다 -
+------------------+-------------+ | StudentFirstName | Total_Count | +------------------+-------------+ | John | 10 | | Larry | 10 | | Johnny | 10 | | Chris | 10 | | David | 10 | | James | 10 | | Jace | 10 | | Mike | 10 | | Robert | 10 | | Mike | 10 | +------------------+-------------+ 10 rows in set (0.03 sec)