如何在MySQL中将Count(*)实现为变量以显示表中的记录数?
别名可以在MySQL中用作变量名,如以下语法所示-
select count(*) AS anyAliasName from yourTableName;
让我们首先创建一个表-
mysql> create table DemoTable695 ( FirstName varchar(100) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable695 values('Chris'); mysql> insert into DemoTable695 values('Robert'); mysql> insert into DemoTable695 values('David'); mysql> insert into DemoTable695 values('Mike');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable695;
这将产生以下输出-
+-----------+ | FirstName | +-----------+ | Chris | | Robert | | David | | Mike | +-----------+ 4 rows in set (0.00 sec)
以下是在MySQL中将count(*)视为变量的查询-
mysql> select count(*) AS TOTAL_NO_OF_RECORDS from DemoTable695 ;
这将产生以下输出-
+---------------------+ | TOTAL_NO_OF_RECORDS | +---------------------+ | 4 | +---------------------+ 1 row in set (0.02 sec)