Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

MySQL: Selecting keys where column douplicate

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".
SOLUTION
Avatar of Jagadishwor Dulal
Jagadishwor Dulal
Flag of Nepal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hankknight

ASKER

That returns an error:
Error: Subquery returns more than 1 row
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial