

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Only return fields which appear 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)
- Related Questions & Answers
- MongoDB query to update only certain fields?
- Get count of values that only appear once in a MySQL column?
- MySQL query to return only the rows which are numeric?
- Get number of fields in MySQL table?
- How to return a string repeated N number of times in C#?
- Select two fields and return a sorted array with their distinct values in MongoDB?
- Finding only strings beginning with a number using MySQL Regex?
- Count the number of distinct values in MySQL?
- MySQL query to check if a certain row has only two words?
- Get records in a certain order using MySQL?
- MongoDB query to return only specific fields (phone numbers) in the form of an array?
- Find the next lowest number higher than a certain number in MySQL?
- How to replicate a string for a specified number of times in MySQL?
- Repeating string for specific number of times using JavaScript
- Print all the sum pairs which occur maximum number of times in C++
Advertisements