View Single Post
Old 01-09-2013, 07:54 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
If you want to be able to identify the table row, then *YOU* need to provide a field in the table that will do so. Records in relational databases (of which MySQL is one) do *NOT* have record numbers automatically attached to them.

And easy way to add such a number would be
Code:
   ALTER TABLE moortown5 ADD recordID INT AUTO_INCREMENT PRIMARY KEY
Now... the problem with doing
Code:
SELECT COUNT(*), recordID, OldCompanyName, NewCompanyName FROM moortown5
GROUP BY OldCompanyName, NewCompanyName
HAVING COUNT(*)>1;
is that only one of the duplicate recordid's will be found. (And if you weren't using MySQL, the query wouldn't even be legal.)

I assume that you want to get a list of *ALL* the duplicated recordid's no?

There are a couple of ways to do it, but this may be the best:
Code:
SELECT T.recordID, T.OldCompanyName, T.NewCompanyName 
FROM moortown5 AS T,
    ( SELECT COUNT(*), OldCompanyName, NewCompanyName 
      FROM moortown5
      GROUP BY OldCompanyName, NewCompanyName
      HAVING COUNT(*)>1 ) AS S
WHERE S.OldCompanyName = T.OldCompanyName
AND S.NewCompanyName = T.NewCompanyName
ORDER BY T.recordId
(the last ORDER BY there is arbitrary..change to what works best for you)
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
sitechooser (01-14-2013)