MySQL: Selecting non-unique records

| | Comments (0) | TrackBacks (0)

Scenario: you want to select all records that have a non-unique value in a column. Use a subquery:

SELECT ... FROM ...
WHERE ... IN (
   SELECT ... FROM ...
   GROUP BY ... HAVING COUNT(*) > 1
);

So, to give an example, you want to select all rows from the table "books" that have a non-unique title:

SELECT * FROM books
WHERE title IN (
   SELECT title FROM books
   GROUP BY title HAVING COUNT(*) > 1
);

0 TrackBacks

Listed below are links to blogs that reference this entry: MySQL: Selecting non-unique records.

TrackBack URL for this entry: http://www.robbiebow.co.uk/mt/mt-tb.cgi/68

Leave a comment

About this Entry

This page contains a single entry by Robbie Bow published on August 7, 2008 10:22 AM.

Creating unix times from datetime stamps was the previous entry in this blog.

Add Options to a Select with jQuery : IE7 is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Powered by Movable Type 4.21-en