Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql selecting unique values

I am really stuck with an sql query... I hope someone can help shed some light for me.

Here is what my table looks like

mysql> show fields from france_data;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int(11)     | NO   | PRI | NULL    | auto_increment |
| email    | varchar(45) | YES  |     | NULL    |                |
| name     | varchar(45) | YES  |     | NULL    |                |
| lastname | varchar(45) | YES  |     | NULL    |                |
| quality  | varchar(45) | YES  |     | NULL    |                |
| country  | varchar(45) | YES  |     | NULL    |                |
| state    | varchar(45) | YES  |     | NULL    |                |
| year     | varchar(45) | YES  |     | NULL    |                |
| owner    | varchar(45) | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
9 rows in set (0.00 sec)

Here's the catch, I have duplicate data in my table, I would like to pull out all data from this table, non-duplicated based on the email.

I ran a simple count like this:

mysql> select count(*) from france_data;

and this is the result set:

+----------+
| count(*) |
+----------+
|  2405259 |
+----------+
1 row in set (0.01 sec)

Now I tried to run a count like this:

mysql> select count(*) from france_data group by email;

Just to see how many unique records I have. Unfortunately this times out.

Does any one know how I can do a count of unique rows and select of the same type?

like image 696
Beyerz Avatar asked Mar 20 '12 06:03

Beyerz


People also ask

How do you SELECT unique values?

In Excel, there are several ways to filter for unique values—or remove duplicate values: To filter for unique values, click Data > Sort & Filter > Advanced. To remove duplicate values, click Data > Data Tools > Remove Duplicates.

What is SELECT distinct in MySQL?

The SELECT DISTINCT statement is used to return only distinct (different) values. Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.

How do I SELECT unique records in SQL?

The unique values are fetched when we use the distinct keyword. SELECT DISTINCT returns only distinct (different) values. DISTINCT eliminates duplicate records from the table. DISTINCT can be used with aggregates: COUNT, AVG, MAX, etc.

Can we use distinct in MySQL?

We can use the MySQL DISTINCT clause to return a single field that removes the duplicates from the result set. For example: SELECT DISTINCT state FROM customers; This MySQL DISTINCT example would return all unique state values from the customers table.


1 Answers

Please try this

SELECT COUNT(DISTINCT email) FROM france_data 
like image 68
punny Avatar answered Nov 01 '22 09:11

punny