1

I have a table like this

CREATE TABLE IF NOT EXISTS users_groups(
  id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id INT(11) UNSIGNED NOT NULL,
  name VARCHAR(250) NOT NULL,
  is_deleted BOOLEAN NOT NULL DEFAULT false,
  creation_date DATETIME NOT NULL,
  last_modification_date DATETIME NOT NULL,
  PRIMARY KEY (id)
);

And I would like to turn the columns (user_id, name) as unique, but the problem I have is that I already have duplicates in that columns. In some cases the columns are repeated many times.

I would like to write a query to update the duplicates rows concatenating some value in the name, for example concatening '*' for the first duplicate '**' for the second duplicate, and so on.

Is this possible writing a query?

I can get the group that I want to modify, but I am not sure if it is possible to achieve what I want with mysql

select users_groups.id, users_groups.user_id, users_groups.name
from users_groups
inner join (select user_id, name, count(*) from users_groups group by user_id, name having count(*) > 1) as u on u.user_id = users_groups.user_id
where users_groups.name = u.name;

2 Answers 2

1

Try this.

SET @prev_user_id := '';
SET @prev_name := '';
SET @cnt := 0;

select s.id, s.user_id, s.name
from 
(
  select 
    users_groups.id,     
    IF(@prev_user_id <> users_groups.user_id OR @prev_name <> users_groups.name, @cnt := 0, @cnt := @cnt + 1) AS cnt,   
    CONCAT(users_groups.user_id, REPEAT('*', @cnt)) AS user_id,
    CONCAT(users_groups.name, REPEAT('*', @cnt)) AS name,  
    @prev_user_id := users_groups.user_id,
    @prev_name := users_groups.name
  from 
    users_groups
) s

Check here: http://sqlfiddle.com/#!9/6d553/10

Sign up to request clarification or add additional context in comments.

3 Comments

thank you for your answer. It did not work, I am not sure why, but it does not recognize the variable @cnt in the select statement.
@agusgambina sorry there's wrong text used, try again.
thank you it worked, I only need to add the update statement between the set and the select and add the where clause for the id at the end.
0

I'm not sure if this helps, but you can make a column unique without it having to be the primary key of that table.

For example:

CREATE TABLE user (
    user_id NOT NULL,
    firstname varchar(255) NOT NULL,
    UNIQUE (firstname)
) 

Or the following statement which creates a unique constraint across multiple columns.

CREATE TABLE user (
    user_id NOT NULL,
    firstname varchar(255) NOT NULL,
    CONSTRAINT constraint_name UNIQUE (user_id, firstname)
) 

Also, I'm not entirely sure why you would want to identify duplicates. It's good practice to avoid duplicate records and data entries entirely. This can be done through table normalization, as well as the use of appropriate indexing.

I hope this helps in some way.

2 Comments

I may have misunderstood your question. However, it's good practice to avoid duplicates entirely. If you make use of proper indexing and keys then you will never have a completely duplicate row. You may well have columns in a row that are duplicate to other rows, but you wouldn't use these rows to identify one single record.
@Chris_Mulheron thank you for your answer. I totally agree with you, but it is something that already happened and I have to fix it.

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.