-2

I have a table with the following columns:

ID
Link
price

There are some duplicates in column Link, how can I get a list of duplicates.

My query:

SELECT * 
FROM tbl
WHERE Link
IN
(SELECT Link
FROM tbl
GROUP BY Link
HAVING CONT(*) > 1;

It returns all the fields with duplicates, but I need to list them, so I can see all of them (fields with duplicates).

fabrik
  • 14,094
  • 8
  • 55
  • 71

3 Answers3

0

Try this:

  SELECT Link
    FROM tbl
GROUP BY Link
HAVING COUNT(*) > 1
Marko Juvančič
  • 5,792
  • 1
  • 25
  • 41
0

You could just do:

SELECT Link, COUNT(*)
FROM tbl
GROUP BY Link
HAVING COUNT(*)>1

In MySQL, you can even leave out the COUNT(*) from the SELECT.

SELECT Link
FROM tbl
GROUP BY Link
HAVING COUNT(*)>1
slaakso
  • 8,331
  • 2
  • 16
  • 27
  • it returns just a row with duplicates but i need to get all duplicates like : ID1 LINK1 PRISE1 ID2 LINK1 PRISE2 ID3 LINK3 PRISE3 ID4 LINK3 PRISE4 – Bohdan Semko Sep 13 '18 at 09:40
  • In that case the query in your question shout produce the result you want (you are just missing the closing parentheses). What is wrong with the output of your original query? – slaakso Sep 13 '18 at 09:49
  • It returns a result like: ID1 LINK1 PRISE1; ID3 LINK3 PRISE3; But not all list. – Bohdan Semko Sep 13 '18 at 09:54
  • And what would the "all list" look like? – slaakso Sep 13 '18 at 10:04
  • ID1 LINK1 PRISE1; ID2 LINK1 PRISE2; ID3 LINK3 PRISE3; ID4 LINK3 PRISE4; – Bohdan Semko Sep 13 '18 at 10:07
  • Your query will list all rows which have Link-column values that appear more than once in the data (="all list"). Please provide sample data, your query and the desired results. – slaakso Sep 13 '18 at 10:26
0

If you want the full rows, then I would recommend:

select t.*
from tbl t
where exists (select 1
              from tbl t2
              where t2.link = t.link and t2.id <> t.id
             );

This assumes that id is unique, which seems like a reasonable assumption.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786