用于查找具有相同列值的多行的SQL查询

时间:2022-12-22 09:06:30

I have a users table with a column called email and a column called enabled. I'm looking to find all users with the same email. If one of these users has enabled as nil, I would like to reset that user's login to its first name (another column). Note: There should be at most two users that share an email.

我有一个带有名为email的列的用户表和一个名为enabled的列。我希望找到所有用户使用相同的电子邮件。如果其中一个用户已启用为nil,我想将该用户的登录名重置为其名字(另一列)。注意:共享最多应有两个用户共享电子邮件。

I'm just starting to learn SQL. How would I get this query working? I appreciate the patience!

我刚刚开始学习SQL。我如何使这个查询工作?我很感激耐心!

update users
set users.login = users.first_name
from users u1 inner join users u2
on u1.email = u2.email 
  where u1.id != u2.id
  and u2.enabled is null

My RDBMS is MySQL!

我的RDBMS是MySQL!

1 个解决方案

#1


0  

If your are using MySQL then;

如果你正在使用MySQL那么;

update users u1 
join users u2 on
u1.email = u2.email and u1.enable is null
set u1.login = u1.first_name

sql fiddle demo

sql小提琴演示

#1


0  

If your are using MySQL then;

如果你正在使用MySQL那么;

update users u1 
join users u2 on
u1.email = u2.email and u1.enable is null
set u1.login = u1.first_name

sql fiddle demo

sql小提琴演示