Solved

MySQL: Selecting keys where column douplicate

Posted on 2014-01-27
5
408 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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). …
Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

20 Experts available now in Live!

Get 1:1 Help Now