Get Rid of MySQL Duplicates with SELECT DISTINCT

Well, there you have it! How do you get rid of duplicate content showing up in your nicely crafted, database-driven tables? Use the DISTINCT clarifier in your SELECT statement.

There’s no need to say more about this easy way to delete doubled data, but an example is always nice for clarity. Let’s say you’re keeping track of all the animals with whom you’ve shared your abode. The database is a simple one that collects information about your pets, their names, the type of animals, birthdates, deaths, and the history you’ve shared.

Here’s an example table produced with a simple SELECT statement. A row of data is repeated in the table because of duplicate values in the database.

Duplicate Data

Query: SELECT name, species, variety, history FROM pets WHERE history = ‘found stray’

Here’s a table produced with a SELECT DISTINCT statement. Each row is distinct and the repeated row has quietly disappeared.

Distinct Data

Query: SELECT DISTINCT name, species, variety, history FROM pets WHERE history = ‘found stray’

How did the data for ‘Highway’ get in there twice? Someone probably re-entered his information that was previously entered at an earlier date. But, now that we know how to use the DISTINCT clarifier in a MySQL query, the duplicated data is not a problem for us in presenting the data onscreen.

Posted in mysql | Leave a comment

Improve Your Skills with a $5 eBook from Packt

That’s right! Visit Packt’s $5 eBook Bonanza until January 3rd, 2014 to pick up some new skills. You’ll be able to get any eBook or Video from Packt for just $5. This includes their entire range of WordPress eBooks as well.

$5-ebooks
Packt Publishing offers more than 1,700 books and videos on a wide range of topics that you might be interested in. Here’s their shopping categories and the number of titles in each category: Continue reading

Posted in WordPress | Tagged | Leave a comment