-1

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..

Lajos Veres
  • 13,595
  • 7
  • 43
  • 56
user2704687
  • 185
  • 3
  • 11
  • Yes. It's possible - although the rules for roman numerals are a little cumbersome – Strawberry Nov 22 '13 at 21:37
  • In software the answer to almost all 'is it possible' questions is 'yes'. This is no exception. The real question is 'how much does it cost?' Try something, post the code if you get stuck –  Nov 22 '13 at 21:37

2 Answers2

1

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...

Denis de Bernardy
  • 75,850
  • 13
  • 131
  • 154
1

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...

Lajos Veres
  • 13,595
  • 7
  • 43
  • 56