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

有帮助吗?

解决方案

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

其他提示

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top