1

My following query doesn't return duplicate records(Model_Number), is there a reason.Should return all duplicate model_number, with the following query it returns all unique.The Mysql has 2,17,092 records in total

SELECT AZ_Code, Model_Number
FROM dumpdata
GROUP BY Model_Number
HAVING COUNT( * ) >1
2
  • Since you are grouping it does returned the duplicates but grouped(those unique model_number are in fact the ones which are duplicates).If you want too see the duplicates as they are JOIN on this subquery Commented Jun 9, 2016 at 16:53
  • @Mihai I do not understand what do you mean by "As they are JOIN on this subquery " Commented Jun 9, 2016 at 16:55

3 Answers 3

1

The previous answers should work, but I would expect this one to be much faster (as MySQL usually JOINs to subqueries more effectively than it uses them elsewhere, such as in WHERE clauses).

SELECT dd.AZ_Code, dd.Model_Number
FROM dumpdata AS dd
INNER JOIN (
   SELECT   Model_Number
   FROM     dumpdata
   GROUP BY Model_Number
   HAVING COUNT(*) > 1
) AS repeatedModels 
ON dd.Model_Number = repeatedModels.Model_Number
Sign up to request clarification or add additional context in comments.

5 Comments

I get an error when I run the query #1052 - Column 'Model_Number' in field list is ambiguous
Have you indexed Model_Number? and what is the field's data type?
let me reboot the server and try it again
Model_Number - varchar(255)
When dealing with larger numbers of rows, indexing can help with grouping, ordering, conditions in WHERE clauses and used for JOINs; and when dealing with character data the benefits can be even more pronounced; comparing 2 million unindexed integers is a lot faster than comparing 2 million strings.
0

The group by clause means you want just one result per unique value you group by (and the fact you can query fields not in it is a MySQL oddity). You could work around with with the use of the in operator:

SELECT AZ_Code, Model_Number
FROM   dumpdata
WHERE  Model_Number IN (SELECT   Model_Number
                        FROM     dumpdata
                        GROUP BY Model_Number
                        HAVING   COUNT(*) > 1)

1 Comment

This query freezes phpmyadmin is there another way to run the query and export to csv
0

One way of seeing the duplicates

SELECT d.AZ_Code, d.Model_Number
FROM dumpdata d
WHERE
(SELECT COUNT(*) FROM dumpdata  WHERE d.Model_Number=dumpdata.Model_Number)>1

3 Comments

This query freezes phpmyadmin, is there another way to run the query
@user580950 How many rows do you have?Also you have to be careful,cancelling a query in phpmyadmin doesnt means the query will also be cancelled on the server.
2,17,092 records in total, I rebooted the server so that phpmyadmin works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.