MySQL查询选择列值仅为0的行,按另一列分组?
为此,请使用分组依据。让我们首先创建一个表-
mysql> create table DemoTable1344 -> ( -> `SequenceId` int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> ClientId int, -> isMarried tinyint(1) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1344(ClientId,isMarried) values(4567,0); mysql> insert into DemoTable1344(ClientId,isMarried) values(9876,0); mysql> insert into DemoTable1344(ClientId,isMarried) values(5432,1); mysql> insert into DemoTable1344(ClientId,isMarried) values(4567,1); mysql> insert into DemoTable1344(ClientId,isMarried) values(9873,0);
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1344;
这将产生以下输出-
+------------+----------+-----------+ | SequenceId | ClientId | isMarried | +------------+----------+-----------+ | 1 | 4567 | 0 | | 2 | 9876 | 0 | | 3 | 5432 | 1 | | 4 | 4567 | 1 | | 5 | 9873 | 0 | +------------+----------+-----------+ 5 rows in set (0.00 sec)
以下是查询以选择列值仅为0的行,并按另一列分组-
mysql> select SequenceId,ClientId,isMarried -> from DemoTable1344 -> group by ClientId -> having isMarried=0;
这将产生以下输出-
+------------+----------+-----------+ | SequenceId | ClientId | isMarried | +------------+----------+-----------+ | 1 | 4567 | 0 | | 2 | 9876 | 0 | | 5 | 9873 | 0 | +------------+----------+-----------+ 3 rows in set (0.00 sec)