要返回出现一定次数的字段,语法如下:
select distinct yourColumnName, count(yourColumnName) from yourTableName where yourColumnName LIKE 'J%' group by yourColumnName having count(*) > 1 order by yourColumnName;
让我们首先创建一个表-
mysql> create table DemoTable1500 -> ( -> Name varchar(20) -> );
使用插入命令在表中插入一些记录-
Mysql> insert into DemoTable1500 values(‘Adam’); mysql> insert into DemoTable1500 values('John'); mysql> insert into DemoTable1500 values('Mike'); mysql> insert into DemoTable1500 values('John'); mysql> insert into DemoTable1500 values('Jace'); mysql> insert into DemoTable1500 values('Jace'); mysql> insert into DemoTable1500 values('Jackie');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1500;
这将产生以下输出-
+--------+ | Name | +--------+ | Adam | | John | | Mike | | John | | Jace | | Jace | | Jackie | +--------+ 7 rows in set (0.00 sec)
以下是查询以返回使用MySQL DISTINCT出现一定次数的字段-
mysql> select distinct Name, count(Name) -> from DemoTable1500 -> where Name LIKE 'J%' -> group by Name -> having count(*) > 1 -> order by Name;
这将产生以下输出-
+------+-------------+ | Name | count(Name) | +------+-------------+ | Jace | 2 | | John | 2 | +------+-------------+ 2 rows in set (0.00 sec)