SQL Update Query – How to Update One Column with Another’s Value

queryupdate

I've found a lot of similar questions, but not exactly what I need. I want to make a SQL command to find all duplicate email rows and update the notes of them with 'do not email' if it is written in the note of one of them.

In the example below I would like to update row 2,4 and 6 with the words "do not email" in the note.

ID | email | note
--------------------
1 | A | do not email
2 | B |
3 | C |
4 | A |
5 | D |
6 | A |
7 | B | do not email

Does anyone have any hints?

Best Answer

This will update all rows in the target table that match an email address in the same table that has been marked as 'do not email'

update a
   set a.note = 'do not email'
  from tbl as a
  join (select distinct email
          from tbl 
         where note = 'do not email'
       ) as b 
    on a.email = b.email
 where a.note <> 'do not email' 
    or a.note is null;