简体   繁体   中英

SQL Query to Find Multiple Rows with Same Column Value

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.

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

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!

If your are using MySQL then;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM