Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MySQL: Finding Oldest Douplicate

Posted on 2014-02-11
3
Medium Priority
?
266 Views
Last Modified: 2014-02-11
The returns the bookkey and updated time of duplicate items:
SELECT elqBooks.isbn, elqBooks.updated
 FROM elqBooks 
 GROUP BY elqBooks.ISBN HAVING count(elqBooks.isbn) > 1

Open in new window

I want the OLDEST item to be selected.  How can I get the OLDEST matching duplicated item?
0
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 2000 total points
ID: 39850012
SELECT elqBooks.isbn, MIN(elqBooks.updated)
 FROM elqBooks
 GROUP BY elqBooks.ISBN HAVING count(elqBooks.isbn) > 1
0
 
LVL 16

Author Comment

by:hankknight
ID: 39850068
Thanks.  Now how can I do this with the bookkey only?  I only want the bookkey of the oldest douplicate record to be included.
SELECT elqBooks.bookkey
 FROM elqBooks 
 GROUP BY elqBooks.ISBN HAVING count(elqBooks.isbn) > 1

Open in new window

0
 
LVL 11

Expert Comment

by:John_Vidmar
ID: 39850188
Without knowing your schema, would you be able to take the results of the group-by/min and join back to your table to get the key?
SELECT	a.bookkey
FROM	elqBooks	a
JOIN	(	SELECT	isbn
		,	updated = MIN(updated)
		FROM	elqBooks 
		GROUP
		BY	isbn
		HAVING	count(*) > 1 
	)	b	ON	a.isbn = b.isbn
			AND	a.updated = b.updated

Open in new window

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating and Managing Databases with phpMyAdmin in cPanel.
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question