仅使用 MySQL DISTINCT 返回出现一定次数的字段?
要返回出现一定次数的字段,语法如下 -
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) -> ); Query OK, 0 rows affected (0.86 sec)
使用 insert 命令在表中插入一些记录 -
Mysql> insert into DemoTable1500 values(‘Adam’); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable1500 values('John'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable1500 values('Mike'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable1500 values('John'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable1500 values('Jace'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1500 values('Jace'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1500 values('Jackie'); Query OK, 1 row affected (0.06 sec)
使用 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)
广告