hankknight
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
I only want it to return 1 column: "bookkey".
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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);
A cleaned-up version:SELECT bookkey FROM elqBooks GROUP BY ISBN HAVING count(*) > 1;
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;
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Error: Subquery returns more than 1 row