要了解with子句的分组依据,让我们创建一个表。创建表的查询如下-
mysql> create table GroupByWithWhereClause -> ( -> Id int NOT NULL AUTO_INCREMENT, -> IsDeleted tinyint(1), -> MoneyStatus varchar(20), -> UserId int, -> PRIMARY KEY(Id) -> );
现在,您可以使用insert命令在表中插入一些记录。查询如下-
mysql> insert into GroupByWithWhereClause(IsDeleted,MoneyStatus,UserId) values(0,'Undone',101); mysql> insert into GroupByWithWhereClause(IsDeleted,MoneyStatus,UserId) values(0,'done',101); mysql> insert into GroupByWithWhereClause(IsDeleted,MoneyStatus,UserId) values(0,'done',101); mysql> insert into GroupByWithWhereClause(IsDeleted,MoneyStatus,UserId) values(0,'done',102); mysql> insert into GroupByWithWhereClause(IsDeleted,MoneyStatus,UserId) values(1,'Undone',102); mysql> insert into GroupByWithWhereClause(IsDeleted,MoneyStatus,UserId) values(1,'done',102); mysql> insert into GroupByWithWhereClause(IsDeleted,MoneyStatus,UserId) values(0,'Undone',103); mysql> insert into GroupByWithWhereClause(IsDeleted,MoneyStatus,UserId) values(0,'done',103); mysql> insert into GroupByWithWhereClause(IsDeleted,MoneyStatus,UserId) values(0,'done',103); mysql> insert into GroupByWithWhereClause(IsDeleted,MoneyStatus,UserId) values(0,'done',103); mysql> insert into GroupByWithWhereClause(IsDeleted,MoneyStatus,UserId) values(0,'done',104); mysql> insert into GroupByWithWhereClause(IsDeleted,MoneyStatus,UserId) values(0,'Undone',104); mysql> insert into GroupByWithWhereClause(IsDeleted,MoneyStatus,UserId) values(1,'Undone',105); mysql> insert into GroupByWithWhereClause(IsDeleted,MoneyStatus,UserId) values(1,'done',105); mysql> insert into GroupByWithWhereClause(IsDeleted,MoneyStatus,UserId) values(1,'done',105); mysql> insert into GroupByWithWhereClause(IsDeleted,MoneyStatus,UserId) values(0,'done',105); mysql> insert into GroupByWithWhereClause(IsDeleted,MoneyStatus,UserId) values(0,'Undone',106); mysql> insert into GroupByWithWhereClause(IsDeleted,MoneyStatus,UserId) values(0,'done',106); mysql> insert into GroupByWithWhereClause(IsDeleted,MoneyStatus,UserId) values(0,'done',106);
使用select语句显示表中的所有记录。
查询如下-
mysql> select *from GroupByWithWhereClause;
以下是输出-
+----+-----------+-------------+--------+ | Id | IsDeleted | MoneyStatus | UserId | +----+-----------+-------------+--------+ | 1 | 0 | Undone | 101 | | 2 | 0 | done | 101 | | 3 | 0 | done | 101 | | 4 | 0 | done | 102 | | 5 | 1 | Undone | 102 | | 6 | 1 | done | 102 | | 7 | 0 | Undone | 103 | | 8 | 0 | done | 103 | | 9 | 0 | done | 103 | | 10 | 0 | done | 103 | | 11 | 0 | done | 104 | | 12 | 0 | Undone | 104 | | 13 | 1 | Undone | 105 | | 14 | 1 | done | 105 | | 15 | 1 | done | 105 | | 16 | 0 | done | 105 | | 17 | 0 | Undone | 106 | | 18 | 0 | done | 106 | | 19 | 0 | done | 106 | +----+-----------+-------------+--------+ 19 rows in set (0.00 sec)
这是对带有WHERE子句的GROUP BY的查询-
mysql> SELECT * FROM GroupByWithWhereClause -> WHERE IsDeleted= 0 AND MoneyStatus= 'done' -> GROUP BY SUBSTR(UserId,1,3) -> HAVING COUNT(*) > 1 -> ORDER BY Id DESC;
以下是输出-
+----+-----------+-------------+--------+ | Id | IsDeleted | MoneyStatus | UserId | +----+-----------+-------------+--------+ | 18 | 0 | done | 106 | | 8 | 0 | done | 103 | | 2 | 0 | done | 101 | +----+-----------+-------------+--------+ 3 rows in set (0.00 sec)