Solved

MySQL: Selecting keys where column douplicate

Posted on 2014-01-27
5
418 Views
Last Modified: 2014-02-10
This returns 3 columns:
SELECT bookkey, isbn, count(isbn) as cnt FROM elqBooks GROUP BY ISBN HAVING cnt > 1

Open in new window

I only want it to return 1 column: "bookkey".
0
Comment
Question by:hankknight
5 Comments
 
LVL 15

Assisted Solution

by:Jagadishwor Dulal
Jagadishwor Dulal earned 50 total points
ID: 39814030
Try Using Nested Query Like:

SELECT bookkey FROM elqBooks WHERE( SELECT count(isbn) as cnt FROM elqBooks GROUP BY ISBN HAVING cnt > 1)
                

Open in new window

0
 
LVL 16

Author Comment

by:hankknight
ID: 39814032
That returns an error:
Error: Subquery returns more than 1 row
0
 
LVL 40

Accepted Solution

by:
Sharath earned 300 total points
ID: 39814278
Are you sure your original query is not giving any error?

You have bookkey in SELECT clause but not in GROUP BY clause.

Anyway, try this.
SELECT bookkey 
FROM elqBooks 
GROUP BY book key,ISBN 
HAVING count(isbn) > 1

Open in new window

0
 
LVL 8

Expert Comment

by:Surrano
ID: 39814295
Well first of all, select you can always the first field of your original query:
SELECT bookkey from 
(SELECT bookkey, isbn, count(isbn) as cnt FROM elqBooks GROUP BY ISBN HAVING cnt > 1);

Open in new window

A cleaned-up version:
SELECT bookkey FROM elqBooks GROUP BY ISBN HAVING count(*) > 1;

Open in new window

but it should not work (nor your original query; did you try it?) since you aggregate without grouping by bookkey. So what is it *exactly* that you'd like to achieve?

-- for all ISBNs that appear more than once in the table, list all bookkeys?
SELECT bookkey from elqBooks 
where isbn in (select isbn from elqBooks group by isbn having count(*)>1);

-- list all bookkeys that appear more than once in the table?
SELECT bookkey FROM elqBooks GROUP BY bookkey HAVING count(*) > 1;

-- for all (bookkey, ISBN) pairs that appear more than once, list the bookkey only but not the isbn?
SELECT bookkey FROM elqBooks GROUP BY bookkey, ISBN HAVING count(*) > 1;

Open in new window

0
 
LVL 8

Assisted Solution

by:Surrano
Surrano earned 150 total points
ID: 39814309
OMG your original query works. MySQL is soooo broken I can't believe it. I wonder if some other expert could explain me how it was a clever idea from the creators of this concept.

In standard SQL, a query that includes a GROUP BY clause cannot refer to nonaggregated columns in the select list that are not named in the GROUP BY clause. For example, this query is illegal in standard SQL because the name column in the select list does not appear in the GROUP BY:

SELECT o.custid, c.name, MAX(o.payment)
  FROM orders AS o, customers AS c
  WHERE o.custid = c.custid
  GROUP BY o.custid;

Open in new window


For the query to be legal, the name column must be omitted from the select list or named in the GROUP BY clause.

MySQL extends the use of GROUP BY so that the select list can refer to nonaggregated columns not named in the GROUP BY clause. This means that the preceding query is legal in MySQL. You can use this feature to get better performance by avoiding unnecessary column sorting and grouping. However, this is useful primarily when all values in each nonaggregated column not named in the GROUP BY are the same for each group. The server is free to choose any value from each group, so unless they are the same, the values chosen are indeterminate.

To summarise:
- it is not standard SQL, so not portable
- you can't predict which value will be printed if there are separate values, unless you sort or do some other trick (which makes mysql's reasoning void on this concept)
- so don't use unaggregated columns.

If you want *one* bookkey for each group which is what mysql does, a *standard sql* way of doing this is:
select min(bookkey)  FROM elqBooks GROUP BY ISBN having count(*)>1;

Open in new window

0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Fore-Foreword Today (2016) Maxmind has a new approach to the distribution of its data sets.  This article may be obsolete.  Instead of using the examples here, have a look at the MaxMind API (https://www.maxmind.com/en/geolite2-developer-package). …
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

948 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now