如何删除 MySQL 中的重复数据

How to remove duplicate data in MySQL

我有 table user 这样的

UserID   | UserName | UserEmail       | FlagUser
1        | Penny    | penny@gmail.com | A1
2        | Maika    | maika@gmail.com | A1
3        | Laila    | laila@gmail.com | A1
4        | Laura    | laura@gmail.com | A2
5        | Penny    | penny@gmail.com | A2
6        | Maika    | maika@gmail.com | A2

我想要这样的结果,如果用户有多个数据,我想用 FlagUser A2

获取用户
UserID   | UserName | UserEmail       | FlagUser
3        | Laila    | laila@gmail.com | A1
4        | Laura    | laura@gmail.com | A2
5        | Penny    | penny@gmail.com | A2
6        | Maika    | maika@gmail.com | A2
DELETE t1
FROM user t1
JOIN user t2 USING (UserName)
WHERE t1.FlagUser = 'A1'
  AND t2.FlagUser = 'A2'

我不相信这个要求是完全明确的,但我猜你正在寻找这样的东西:

SELECT x.* 
  FROM my_table x 
  LEFT 
  JOIN my_table y 
    ON y.username = x.username 
   AND y.flaguser = 'A2' 
   AND y.user_id <> x.user_id 
 WHERE y.user_id IS NULL;

如果您使用的是 mysql 8.0,那么 row_number() WITH Common table expression 是方便的方式:

架构:

 create table user(UserID int, UserName varchar(50), UserEmail varchar(50), FlagUser varchar(50));
 insert into user values(1,        'Penny'    , 'penny@gmail.com'  ,'A1');
 insert into user values(2,        'Maika'    , 'maika@gmail.com'  ,'A1');
 insert into user values(3,        'Laila'    , 'laila@gmail.com'  ,'A1');
 insert into user values(4,        'Laura'    , 'laura@gmail.com'  ,'A2');
 insert into user values(5,        'Penny'    , 'penny@gmail.com'  ,'A2');
 insert into user values(6,        'Maika'    , 'maika@gmail.com'  ,'A2');

查询

 with cte as
 (
     select *,row_number()over(partition by username order by flaguser desc) rn
     from user
 )
 select * from cte where rn=1

输出:

UserID UserName UserEmail FlagUser rn
3 Laila laila@gmail.com A1 1
4 Laura laura@gmail.com A2 1
6 Maika maika@gmail.com A2 1
5 Penny penny@gmail.com A2 1

db<>fiddle here

您也可以使用 Inner joingroup by 子句得到相同的结果:

 select u.* from user u
 inner join
 (select username,max(flaguser) maxflaguser from user
 group by username) u2
 on u.username=u2.username and u.flaguser=u2.maxflaguser

输出:

UserID UserName UserEmail FlagUser
3 Laila laila@gmail.com A1
4 Laura laura@gmail.com A2
5 Penny penny@gmail.com A2
6 Maika maika@gmail.com A2

或者您可以使用子查询 select 每个用户的单行,最高值为 flaguser:

 Select * from user u
 where flaguser=(select max(flaguser) from user u2 where u.username=u2.username)

输出:

UserID UserName UserEmail FlagUser
3 Laila laila@gmail.com A1
4 Laura laura@gmail.com A2
5 Penny penny@gmail.com A2
6 Maika maika@gmail.com A2

db<>fiddle here

如果 username, flaguser 上只有两个标志和一个索引,那么最快的方法可能是:

select u.*
from users u
where u.flaguser = 'A2' or
      not exists (select 1
                  from users u2
                  where u2.name = u.name and u2.flaguser = 'A2'
                 );