
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Return Fields Appearing a Certain Number of Times Using MySQL DISTINCT
To return fields appearing a certain number of times, the syntax is as follows −
select distinct yourColumnName, count(yourColumnName) from yourTableName where yourColumnName LIKE 'J%' group by yourColumnName having count(*) > 1 order by yourColumnName;
Let us first create a table −
mysql> create table DemoTable1500 -> ( -> Name varchar(20) -> ); Query OK, 0 rows affected (0.86 sec)
Insert some records in the table using insert command −
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)
Display all records from the table using select statement −
mysql> select * from DemoTable1500;
This will produce the following output −
+--------+ | Name | +--------+ | Adam | | John | | Mike | | John | | Jace | | Jace | | Jackie | +--------+ 7 rows in set (0.00 sec)
Following is the query to return fields which appear a certain number of times using MySQL DISTINCT −
mysql> select distinct Name, count(Name) -> from DemoTable1500 -> where Name LIKE 'J%' -> group by Name -> having count(*) > 1 -> order by Name;
This will produce the following output −
+------+-------------+ | Name | count(Name) | +------+-------------+ | Jace | 2 | | John | 2 | +------+-------------+ 2 rows in set (0.00 sec)
Advertisements