让我们首先创建一个表-
mysql> create table DemoTable ( isValidUser boolean );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values(true); mysql> insert into DemoTable values(false); mysql> insert into DemoTable values(false); mysql> insert into DemoTable values(true); mysql> insert into DemoTable values(true); mysql> insert into DemoTable values(true); mysql> insert into DemoTable values(false);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+-------------+ | isValidUser | +-------------+ | 1 | | 0 | | 0 | | 1 | | 1 | | 1 | | 0 | +-------------+ 7 rows in set (0.00 sec)
现在,让我们看到了查询使用COUNT()
,并IF()
在一个MySQL查询-
mysql> select count(if(isValidUser=1,1,NULL)) AS TOTAL_NUMBER_OF_ONE from DemoTable;
这将产生以下输出-
+---------------------+ | TOTAL_NUMBER_OF_ONE | +---------------------+ | 4 | +---------------------+ 1 row in set (0.06 sec)