Question

I know you can select duplicate rows in MYSQL with the following query:

SELECT ID, bedrijfsnaam, plaats, COUNT( * ) 
FROM profiles
GROUP BY bedrijfsnaam, plaats
HAVING COUNT( * ) >1

I want to able to select the duplicates and rename them with PHP. Is that possible? If it finds three duplicates for one 'bedrijfsnaam-plaats'-combination, I want to add 'I', 'II', 'III' at the end of the 'bedrijfsnaam' value. Is that even possible? Is there some kind of loop I could run?

Thanks..

Was it helpful?

Solution

I think these 2 query can be an answer:

create table maptable as 
SELECT
  p2.ID,
  concat(p2.bedrijfsnaam,count(*)) as bedrijfsnaam_new
FROM profiles p
JOIN profiles p2 on p2.bedrijfsnaam = p.bedrijfsnaam
 AND p2.plaats = p.plaats
 AND p2.ID > p.ID
group by p2.id,p2.bedrijfsnaam, p2.plaats;

update
  profiles,
  maptable
set
  profiles.bedrijfsnaam=bedrijfsnaam_new
where
  profiles.id=maptable.id;

The first part is based on Denis' answer...

OTHER TIPS

Instead of counting, you can find the rows that are dups directly:

SELECT DISTINCT p2.ID, p2.bedrijfsnaam, p2.plaats
FROM profiles p
JOIN profiles p2 on p2.bedrijfsnaam = p.bedrijfsnaam
 AND p2.plaats = p.plaats
 AND p2.ID > p.ID          -- this is where the magic occurs

Then run an update accordingly...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top